PHP variable data passing
How can we pass variables from one page to another using PHP.
You can pass variables from one page to another using the following ways:
- $_REQUEST
- $_POST
- $_GET
- $_COOKIE
- $_SESSION
Note
- REQUEST, POST and GET pass variables or information from one page to another
- COOKIE and SESSION pass variables or information around an entire application or website.
These are called PHP super globals. I will only mention their specific uses in terms of web development. I will not go in depth if you need further details please refer to http://www.php.net/
$_REQUEST (URL passing)
This is the easiest one but the only drawback is you can pass max 3 variables and it looks quite messy.
How to set
Just add ? following by the variable name and then the value
Example
<a href="page1.php?name=sam&age=12&gender=m">click here </a>
How to retrieve
Use PHP super global variable $_REQUEST to retrieve the values.
Example
$name=$_REQUEST["name"];
$age=$_REQUEST["age"];
$gender=$_REQUEST["gender"];
$_POST (HTML form)
Use html forms to pass information
How to set
Example
<form action="page1.php" method="post">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
Gender: <input type="text" name="gender">
<input type="submit" name="submit">
</form>
How to retrieve
Simple use the $_POST variable to retrieve
Example
$name=$_POST["name"];
$age=$_POST["age"];
$gender=$_POST["gender"];
$_GET (HTML form)
Use html forms to pass information
How to set
Example
<form action="page1.php" method="get">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
Gender: <input type="text" name="gender">
<input type="submit" name="submit">
</form>
How to retrieve
Simple use the $_GET variable to retrieve
Example
$name=$_GET["name"];
$age=$_GET["age"];
$gender=$_GET["gender"];
The difference between $_POST and $_GET is that the latter is similar to $_REQUEST and when you use $_GET the variables are visible in the URL just like $_REQUEST and can also be retrieved using $_REQUEST. In other words the GET method adds the variables to the URL when the form is submitted like page1.php becomes page1.php?name=sam&age=12&gender=m
$_COOKIE
cookie stores information on the local computer or client and this information can be accessed on all the pages of an application till desired. Cookies are not very secure but are very useful in making interactive websites. Like the websites that greet you by your names etc
How to set
Syntax
setcookie(name, value, expiration);
Example
setcookie("name", "sam", 3600);
setcookie("age", "12", 3600);
setcookie("gender", "m", 3600);
The expiration is in seconds so 60×60=3600sec = 1 hr. So the cookies do not expire until one hour or make that 1 day or 1 year++
How to retrieve
Example
$name=$_COOKIE["name"];
$age=$_COOKIE["age"];
$gender=$_COOKIE["gender"];
How to destroy manually
setcookie("name", "sam", -3600);
setcookie("age", "12", -3600);
setcookie("gender", "m", -3600);
$_SESSION
Sessions stores information on the server and this information can be accessed on all the pages on an application. The sessions are usually destroyed when a user leaves an application. Sessions have security loop holes but are safer than cookies. Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users’ data from getting confused with one another when visiting the same webpage
How to set
session_start(); //this has to be added at the very beginning of the page
$_SESSION["name"]="sam";
$_SESSION["age"]=12;
$_SESSION["gender"]="m";
How to retrieve
Example
session_start();
$name=$_SESSION["name"];
$age=$_SESSION["age"];
$gender=$_SESSION["gender"];
How to destroy manually
session_start();
unset($_SESSION["name"]);
unset($_SESSION["age"]);
unset($_SESSION["gender"]);
or destroy completely in one go
session_start();
session_destroy();
cookie
- created on the client or local computer
- created for a given set of time
session
- created on the server
- usually gets deleted when a user leaves an application