Home Tutorial Php PHP Variables from Page to Page

 
 

Share on Google+Share on Google+
PHP Variables from Page to Page
Posted on: January 13, 2010 at 12:00 AM
Advertisement
A PHP programmer can easily pass the variables from one page to another page by using different method and functions provided by the PHP Scripting Language. Such as : 1. $_GET[] 2. $_POST[] 3. $_SESSION[]

PHP Variables from Page to Page

A PHP programmer can easily pass the variables from one page to another page by using different method and functions provided by the PHP. Such as :

1. $_GET[]

2. $_POST[]

3. $_SESSION[]

 

1. $_GET[]   

The in-built $_GET[] function in php is used to take the information from one page to another page through URL by mentioning method get. It is not necessary to mention get method in the form because by default it will take the value of variables in get method. The information is sent through URL can be  retrieved because it will be displayed in the address bar of your browser's. Also, it has limitation of information to send through URL. For example :

Example 1. Sending values from URL

page1.php

<html>

<head>

<title>Sending variable from Url</title>

</head>

<body>

    <a href="page2.php?myname=Suraj Mittal&age=22&fav_color=White&fav_fruit=Grapes">Click here to send variables in the URL</a>

</body>

</html>

page2.php

<?php

$name = $_GET['myname'];

$age = $_GET['age'];

$color = $_GET['fav_color'];

$fruit = $_GET['fav_fruit'];

            echo "My name is ".$name.". and I am ".$age." years old. My favourite color is ".$color." and my favourite fruit is ".$fruit.".";

?>

The output of the above example is : My name is Suraj Mittal. and I am 22 years old. My favourite color is White and my favourite fruit is Grapes.

And, the values in the URL shown like this : http://localhost/phpVariable/page2.php?myname=Suraj%20Mittal&age=22&fav_color=White&fav_fruit=Grapes 

If you want to pass any unnecessary information in the URL which can't be misused by anyone is fine but incase if you are sending any confidential information in the URL then it will create trouble for you in near future. Let's see how it will effect you :

Example 2. Sending values from URL through form.

login.php

<html>

<head>

<title>Login Page</title>

</head>

<body>

<form action="welcome.php" method="get">

<table width="300" align="center" cellpadding="2" cellspacing="5" border="1">

<tr>

<td>Username :</td>

<td><input type="text" name="username" /></td>

</tr>

<tr>

<td>Password :</td>

<td><input type="password" name="password" /></td>

</tr>

<tr>

   <td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td>

</tr>

</table>

</form>

</body>

</html>

 

welcome.php

<?php

$uname = $_GET['username'];

$password = $_GET['password'];

echo "Welcome ".$uname."!";

?>

The output of the above example is : Welcome surajmittal@gmail.com!
And, the values in the URL shown like this : http://localhost/phpVariable/welcome.php?username=surajmittal%40gmail.com&password=suraj&submit=Submit

If you have noticed that we have passed only username into the echo but still if you see the URL, password is also visible there. In other words, your whole confidential information are visible in the URL and anyone can retrieve from there. So, it is not the good method while passing confidential information into multiple pages. 

 

2. $_POST[]

The in-built $_POST[] function in php is also used to take the information from one page to another page through URL by mentioning method post in the form. The information is sent to other pages can't be visible for anyone and moreover it has no limits on the amount of information to send. This is most secure method to use it. For example :

Example 1 : Sending values using $_POST[] method.

login.php

<html>

<head>

<title>Login Page</title>

</head>

<body>

<form action="welcome.php" method="post">

<table width="300" align="center" cellpadding="2" cellspacing="5" border="1">

<tr>

<td>Username :</td>

<td><input type="text" name="username" /></td>

</tr>

<tr>

<td>Password :</td>

<td><input type="password" name="password" /></td>

</tr>

<tr>

<td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td>

</tr>

</table>

</form>

</body>

</html>

welcome.php

<?php

$uname = $_POST['username'];

$password = $_POST['password'];

echo "Welcome ".$uname."!";

?>

The output of the above example is : Welcome surajmittal@gmail.com!

And, the values in the URL shown like this : http://localhost/phpVariable/page3.php

If you noticed the URL, there was no confidential information in the address bar. You can use this method to transfer the confidential information from one page to another page.

 

3. $_SESSION[]

The in-built $_SESSION[] function in php is also used to store the information for a specified length of time, and across multiple pages. Suppose, you log into a PHP website a session is created at the same moment with your user information and stored in the $_SESSION variable. Every single site or browser receives a unique session ID (SID) when $_SESSION is called. Lets see the example :

Page1 : login.php

<html>

<head>

<title>Login Page</title>

</head>

<body>

<form action="welcome.php" method="post">

<table width="300" align="center" cellpadding="2" cellspacing="5" border="1">

<tr>

<td>Username :</td>

<td><input type="text" name="username" /></td>

</tr>

<tr>

<td>Password :</td>

<td><input type="password" name="password" /></td>

</tr>

<tr>

<td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td>

</tr>

</table>

</form>

</body>

</html>

Page2 : welcome.php

<?php

session_start();

if($_POST['username']=="admin" || $_POST['password']=="admin")

{

$info['username'] = "admin";

$info['password'] = "admin";

$_SESSION['login'] = $info;

$abc = $_SESSION['login'];

foreach($abc as $key=>$value)

{

echo $key." = ".$value."<br/>";

}

}

else

{

echo "<h4>You are not login!</h4>";

}

?>

In the above example the first page (login.php) have simple html form. In the second page (welcome.php) we used $_SESSION[] function using post method. If you noticed the code in the second page, in the both cases it will take you to the next page but incase if you passed the wrong information it will display the message on the screen i.e. you are not login!.

In the second case where you logged in successfully it will display your username or password.

Advertisement

Related Tags for PHP Variables from Page to Page:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 13, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com