single.php
In every projects we have to pass values from one page to another. In php there are 3 methods available commonly ( More methods may be available ).
Post
Post is one of the common and safest method used. In this method, values are passed to the next page as hidden values. For using this method, first we need to redirect to the next page by post method.
<form method="post" action="second_page_name.php" >
<input type="hidden" name="name_of_value" value=" value_to_transfer ">
</form>
Now the page will be redirected to second_page_name.php
and the value_to_transfer
will be available in the page on a post variable name_of_value
Now let us see how we can retrieve the value.
Declare a variable to store the value. Here the variable is say $temp
.
$temp=$_POST[‘name_of_value’];
Now $temp
is holding the value which we passed.
GET
Get is the second method widely used. Though it is common, it is not safe to use. In this method, the value is passed along with url. In the second page, we extract the value from the url.
Why this method is not safe is, every one can see the values passed. This will help hackers to intrude into the actions and hack the website.
Now lets see how can we pass a value through url
<a href=”second_page.php?email=sample@mail.com”>
Here the second_page.php
is the second page where the value should be passed. sample@mail.com
is the value to be passed.
After the name of the second page, put a ?
. After that we assign a name (here email
) then put =
hen place the value.
Now lets see how can we get the value in the second page.
$temp=$_GET['email'];
Now $temp
is holding the value sample@email.com
.
SESSION
As everyone knows session is a feature of web technology. Let’s see how can we use session to transmit value.
session_start();
$_SESSION[‘email’]=”sample@email.com”;
Now the value got transmitted.
Now to retrieve the value.
session_start();
$temp=$_SESSION[‘email’];
Now $temp
is holding the value.