Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Friday, 21 March 2014

Using html entities for Security In Php

php security
The Article Working with data from, $_POST Variables , $_GET Variables. We seen the from of day, date, and year had taken from user and echo out.

Code for Working With $_GET Variables 


<?php



if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = $_GET['day'];

$date = $_GET['date'];

$year = $_GET['year'];

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot1 , Screenshot2 ):-


using html entities for security in php
Screenshot1
date, time in php
Screenshot2
This example if you want to see in Working with form , $_GET Variables , $_POST variables . In this example we had user input and echo out the variables. 

Now This articles deals with security of the form data. If i want to extracting data from database or submitting data to database then extracting it back and any thing use to submitted. There is always chance the people unwanted things with your codes or with your web application may be now 

For Examples Let see Screenshots3 and Screenshot4
using html entities for security in php
Screenshot3
using html entities for security in php
Screenshot4


You can see the output to the page has now been formatted depending upon what user input in. Now the reasons it is dangerous because of iframe attribute.

<iframe src="pagehere"></iframe>


Output Screenshot5 and Screenshot6

using html entities for security in php
Screenshot5
using html entities for security in php
screenshot6
Now you can see what happen is we written this html tags and its echo down to the page. Therefore we have processing html on the page. We don't want our user to do this and there is simple way to do this there is simple and easiest way to protect against this.

Code for html entities for security

<?php
if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = htmlentities($_GET['day']);

$date = htmlentities($_GET['date']);

$year = htmlentities($_GET['year']);

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output 

using html entities for security in php
screenshot7
entities for security in php
Screenshot8
html entities for security in php
Screenshot9
What we do is when we declare our variables $day, $date and $year. We can also include this wrapped function means $_GET to take variable user inputted to the form before that call function html entities. What htmlentities does shown in screenshots.  As you can see screenshot7 is the page source its html tags of iframe. So, with html entities what we actually doing Now you can see screenshot8 its just displaying text the user type not an html tags it will convert html tags to normal text.

So, thats basic security around your form. Its ensure that your user does not change background change color of background by iframe.

Read More

Working with Form data , $_GET , $_POST Variables Html Forms in Php

Form data variables in php
Working with Form data ,  $_GET , $_POST Variables Html Forms back to basic how we can use it in different elements of in that allow as to process data through php programming language. To sent the data to page In this example i am using same page for posting. You can post same page where you are on currently or at another page. Now let see each elements of html form. Name is something to name the textbox that we can pick up for using and processing user input.



Code for working with Form Data 


<form action = "index.php" method="GET">

Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output

Working with Form data ,  $_GET , $_POST Variables Html Forms in Php
Screenshot1
<from> tag is starting of the form and </from> tag is ending of the form. We also can specify action a method now action is where this form is submit when the submit button is clicked. So, we include submit button in this inside form. Here i want my page to link back to same page to index.php you can also redirect to another page. The method can be GET or POST. Depending upon submitting variables. 

$_GET variable is sending data visible in url sending variable through format of url.

$_POST variable is sending data its send data through http post method.

So, Here i am using GET method then take some input method of text type to display date, time and year. take this value from the user.


Working with $_Get Variables in Php programming language. Html forms we may know html forms are used for getting user input get method is useful for forms. 

Code for Working With $_GET Variables (Screenshot1):-

working with global variables
Screenshot1


<?php



if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

$day = $_GET['day'];

$date = $_GET['date'];

$year = $_GET['year'];

if(!empty($day)&&!empty($date)&&!empty($year)) {

echo 'It is '.$day.' '.$date.''.$year;

}else {

echo 'Fill in all fields.';

}

}

?>



<form action = "index.php" method="GET">



Day:<br><input type="text" name="day"><br>

Date:<br><input type="text" name="date"><br>

Year:<br><input type="text" name="year"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot2):-


working with form data, $_GET, $_POST variables
Screenshot2
creating html form explain the action method variable GET & POST. $_GET and $_POST variable of html form we can use it in php programming language. For picking up forms variable to php variable. In this example we are processing three variable in get method date, date and year. From the help of html forms we need value to pick up and processes to php and display to the user in this example.

Now in php what we need to do is firstly check few conditions 

1. Does It exist? or "Has it been submitted?"

example code
if (isset($_GET['day'])&&isset($_GET['date'])&&isset($_GET['year'])) {

It will check whether user had submitted the form that will check submit value of submit button.

2. Is it empty? or "Does value == NULL"

example code
if(!empty($day)&&!empty($date)&&!empty($year))

It will check whether submitted value is empty or null value.

3. Display back to user

example code

echo 'It is '.$day.' '.$date.''.$year;
}else {
echo 'Fill in all fields.';
}
}

If both condition will true it will output the user what the user inputted in the form values. 

Now you will be thinking how does the values of form detected in php variable 

example code
$day = $_GET['day'];
$date = $_GET['date'];
$year = $_GET['year'];

In this way we can save form values into the php variables. $_GET method is useful to grab the values from the form to the php variables the name of the textbox field can be use to grab the values in $_GET method.

Day:<br><input type="text" name="day"> -> $day = $_GET['day'];.

The attribute name of the html form is used into the $_GET method and then it can be declare had php variables.
$_POST Variables in php
Screenshot3


Working with $_POST Variables 


Post data using forms now we already seen get data using forms using post data is because for security. In $_GET variable we were displaying value of from into the url. We were showing value name of the form and its value in our url in $_GET method of the form. You can see the structure of $_GET method variables in Screenshot2 and Screenshot3. But in $_POST variable the url of the page will be the file name not the values or anything will show to the url its the only difference between $_GET and $_POST variable. The $_POST variable is used in long text area like description of anything , in registration form for password because password must not be pass to the url to show user and url had limitation some amount of text we can show in url therefore for the long description we can not use $_GET variable.

Code for With $_POST Variables :-


<?php



$match = 'pass123';



if(isset($_POST['password'])) {

$password = $_POST['password'];

if(!empty($password)) {

if($password==$match){

echo 'That is correct!';

}else{

echo 'That is incorrect!';

}

}else{

echo 'Please enter a password.';

}

}



?>



<form action="index.php" method="POST">

password:<br>

<input type="password" name="password"><br><br>

<input type="submit" value="submit">

</form>

Output (Screenshot4)

variables in php
Screenshot4
variables in html forms
Screenshot5

In this example i am checking whether user password is matched with the static variable $match. You can see how $_POST variable is used same as $_GET Variable just by replacing word GET to POST. All things are the same here but the major difference is of url in $_GET method url can pass form values as well as $_POST variable just include file name thats it.

Now as you compare screenshot5 and Screenshot screenshot2 url you will come to the whats its happening. 

Read More

Friday, 28 February 2014

Functions with Arguments in php

call by reference
Functions With Arguments in Php Programming Languages. This tutorial is about functions passing arguments to functions.Functions are useful in code to shorten the amount of code to write and you can refer back this functions too which is known as calling of functions and it will process the same code every-time you call.

Code basic Structure of functions (Screenshot1):-

Functions With Arguments In php
Screenshot1

<?php



function name() {

   /// here

}



name();



?>


This is basic and standard structure of functions.

Code for adding numbers by functions (Screenshot2):-

Code for adding numbers by Functions Screenshot2

<?php
function add($number1, $number2) {
echo $number1 + $number2;
}
add();


?>

In this example,I have created a function adding two numbers.You may do programming as it is simply done to add two numbers. You may take user input as number1 and number2 and for that you may need functions to add to numbers together. Always get practiced of using functions instead of structures as its lot better then doing things on the fly. By calling functions specially with arguments you save a lot of time,better and as well as easier to read.
In this example, I am making function add and inside the function specify those arguments which are in the form of variables.Here my variables are $number1 and $number2. Here the variables type are unspecified that whether they are numbers or string which actually doesn't matter.That depends on how you deals everything inside functions.Now echo out that particular results where $number1 will refer to functions variable. The functional arguments must match that is used in the functions. Here let me show you calling functions without arguments to show you errors, and how to read errors.
In output Screenshot3
Now you can see in output warning coming up which is missing argument 1 for add(). Its telling us first argument is missing for function add, it tells us in which line code is and it also tell us where the functions has been declared. And also its missing argument 2 for add() function and code in same line and function is defined in same line and actually it gives the results.

Code for adding two numbers in functions with arguments(Screenshot4):-

Code for adding two numbers in functions with arguments Screenshot4
<?php



$inumber1 = 10;

$inumber2 = 5;



function add($number1, $number2) {

echo $number1 + $number2;

}



add($inumber1,$inumber2);

?>

Output (Screenshot5):-

Functions with arguments in php
Screenshot5
In this example of adding two number we are including some numbers. Taking two variables like inumber1 and inumber2. Now as we created variables $number1= 10 and $number2= 5 so, result is 15.Now when we call function we need to specify that two variables ,this two variables will pass as $inumber1 will be pass through function variable $number1 and $inumber2 will be pass through function variable $number2. That the only arguments we need to pass through functions.Now calling in add() that variables will be interpreted, then inside functions they are processed.then we echo out the results.And you can see results 15.

Code for adding two numbers in functions with arguments(Screenshot6):-

Screenshot6
<?php



$inumber1 = 22;

$inumber2 = 8;



function add($number1, $number2) {

echo $number1 + $number2;

}



add($inumber1,$inumber2);

?>

Output (Screenshot7):-

functions with arguments in php
Screenshot7
In this examples codes are same just changed variables to different integer.

Code for functions with arguments to display date (Screenshot8):-

functions php
Screenshot8
<?php



function displayDate($day, $date, $year) {

echo $day.' '.$date.' '.$year;

}



displayDate('Monday',31,2011);



?>


Output (Screenshot9):-

arguments in php functions
Screenshot10


In this example,I am making functions to print date.Making functions of displaying date will feed in the day , the date and the year.Then echo out the result into the functions.Here our day will be string then date will be integer and year will be integer.Now on calling the function displayDate(), passing variables string as well as integer will be displayed.In this way arguments can be pass in php.
Read More

Thursday, 13 February 2014

Variables in php

php variables
Variables
This tutorial is for variables. If you have worked with any other programming language you know what are variables for and what they will do.Values are assigned to variables and variables are of different types.Now in this tutorials i am going to show you how to declare variables by assigning values to standard structure and then variables are echoed out on the browser.So, Usually in programming languages such as JavaScript or for any other language like it we have to use keywords,for example in java we use “var” like var example.





<?php

var name = 'value';

?>



In php we don’t need any keywords to define anything only we need to assign it with “$” dollar sign notation and afterwards we can assign “_” sign or characters or strings or numeric characters also. Assigning can be done in upper-case and lower-case both.
Code for Variables(Screenshot1) :-

Variables
Code For Variables Screenshot1


<?php

echo $text = 'Hello World.';

echo $number = 100;

?>



output (Screenshot2) :-

Code for Variables
Screenshot2


In Screenshot1 you can see codes for string.I have used sign ” notation for numeric value and i am not using any notation but only semi colon “;” . Semi colon “;” is a separator. Echo statementecho’s out the variables.We can assign any value to variables such as floating values, Boolean values,etc., but this we will see later on in if statements.

Code for variables different way (Screenshot3) :-

Variables in php
Code for Different types for variables Screenshot3


<?php

$text = 'Hello World.';

$number = 100;

 echo $text;

?>


Output (Screenshot4) :-

code for variables in php
Variables Screenshot4

It will only echo variable $text contain because we only echoing $text variable Code for variables different way second example (Screenshot5):-

Screenshot5


<?php

$text = 'Hello world.";

$number = 100;

echo $number;

?>

Output(Screenshot6) :-

Variables string
Screenshot6


It will echo out the content of variable $number because we are echoing variable $number. Programming languages are used todeclare different data’s, for example you want to take users age or take users name or anything you need to declare,so either you will store it as string data or you will store it as numerical data like integers (similarly for floats),which is somewhat tedious ! But, I would like to tell you an important aspect of php , that you don’t need to think about what data you are storing, you just simply need to assign “$” Sign in the prefix of variable name following with equal to and the variable value. Declaration of variables in php is quite easy.

Read More

Embedding PHP Inside HTML

Embedding PHP Inside HTML
Embedding PHP Inside HTML
This is a tutorial i am going to talking about embedding PHP Inside HTML. Now embedding is paying something within something embedding insides. lets see example

Code for simple html form with php file extension (screenshot1):-


Code For Simple Html From With Php File Extension Screenshot1
<input type="text" value=" Hello World.">

output (Screenshot2) :-

Embedding PHP Inside HTML
Screenshot2
you will see a small text-box with echo hello world. Now in another example i will embedding php code with html by using variables of php and in values of html form i will use standard variable for string lets see how it will happens

Code for embedding PHP Inside Html (Screenshot3):-

Code for embedding PHP Inside Html
                         Code for embedding PHP Inside Html Screenshot3


<?php
$text = 'Hello World.';
?>
<input type="text" value="<?php echo $text; ?>">

Output (Screenshot4):-


Embedding PHP Inside HTML
Screenshot4
In Advance php you have to use dynamic forms and so its good example for start-up  now in this $text is the variable which is string Hello world. and in html code we had called $text which is string Hello world. its pretty easy. variables also use in database , static text etc. Its very useful in php. variables are declared with ($) sign. and at end use “;”. and when you write php code u can write in one line or multiple line there is same effects. for example

you can use this code also for same result (screenshot4) :-

<?php
$text = 'hello world.';
?>
<input type="text" value="<?php
echo $text;
?>
">
Read More

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates