PHP Variables from Page to Page


 

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 Scripting Language. Such as : 1. $_GET[] 2. $_POST[] 3. $_SESSION[]

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.

0

login.php

<html>

<head>

1

<title>Login Page</title>

</head>

<body>

2

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

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

<tr>

3

<td>Username :</td>

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

</tr>

4

<tr>

<td>Password :</td>

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

5

</tr>

<tr>

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

6

</tr>

</table>

</form>

7

</body>

</html>

 

8

welcome.php

<?php

$uname = $_GET['username'];

9

$password = $_GET['password'];

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

?>

0

The output of the above example is : Welcome [email protected]!
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. 

 

1

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.

2

login.php

<html>

<head>

3

<title>Login Page</title>

</head>

<body>

4

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

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

<tr>

5

<td>Username :</td>

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

</tr>

6

<tr>

<td>Password :</td>

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

7

</tr>

<tr>

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

8

</tr>

</table>

</form>

9

</body>

</html>

welcome.php

0

<?php

$uname = $_POST['username'];

$password = $_POST['password'];

1

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

?>

The output of the above example is : Welcome [email protected]!

2

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

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

4

<html>

<head>

<title>Login Page</title>

5

</head>

<body>

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

6

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

<tr>

<td>Username :</td>

7

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

</tr>

<tr>

8

<td>Password :</td>

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

</tr>

9

<tr>

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

</tr>

0

</table>

</form>

</body>

1

</html>

Page2 : welcome.php

<?php

2

session_start();

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

{

3

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

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

$_SESSION['login'] = $info;

4

$abc = $_SESSION['login'];

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

{

5

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

}

}

6

else

{

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

7

}

?>

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!.

8

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

Ads