PHP Variable Across Pages


 

PHP Variable Across Pages

PHP provides various techniques to do so, there are many pre-defined variables are used to pass values from one page to another, like $_GET[], $_POST[] etc, these variables store the information.

PHP provides various techniques to do so, there are many pre-defined variables are used to pass values from one page to another, like $_GET[], $_POST[] etc, these variables store the information.

PHP variables across pages

When we develop web applications, sometimes we need to share values among various web pages.

PHP provides various techniques to do so, there are many pre-defined variables are used to pass values from one page to another, like $_GET[], $_POST[] etc, these variables store the information.

1. Example of  $_GET[ ] :

In this example, we use two files first one is  basic html file and the second one where we send the information through $_GET method. By mentioning method in the form we can transfer the data from one page to another. Here, we use $_GET method to send data to the other page. Note that $_GET method send the whole information in the  URL shown in the address bar and it is not advisable to use this method because anyone get your information through URL. For example: If you are signing in any site and you fill your username and password and submit the form, at that moment your personal data will be shown in the address bar and it is an example of security breach.

<html>

<head>

<title>php variables across pages</title>

</head>

<body>

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

Username : <input type="text" name="username" value=""></input>

Password : <input type="password" name="pass" value=""></input>

<input type="submit" name="submit" value="Submit me!"></input>

</form>

</body>

</html>




Welcome.php

<?php

$name = $_GET['username'];

0

$pass = $_GET['pass'];

echo "Your username is $name ."."<br/>";

1

echo "Your password is $pass .";

?>

2

Output:

http://localhost/rakesh/welcome.php?username=rakesh.pandey10%40gmail.com&pass=12365&sumbit=Submit+me!



2. Example of  $_POST[ ]:

Here in this example we use $_POST method to transfer data from one page to another. Data sent from the login form will not be visible in the address bar and it is more secure method than $_GET[].

<html>

3

<head>

<title>php variables across pages</title>

4

</head>

5

<body>

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

6

Username : <input type="text" size="25" name="username" value="" />

7

Password : <input type="password" size="25" name="pass" value="" />

<input type="submit" name="sumbit" value="Submit me!" />

8

</form>

9

</body>

</html>

0



Welcome.php

<?php

1

$name = $_POST['username'];

$pass = $_POST['pass'];

2

echo "Your username is $name ."."<br/>";

3

echo "Your password is $pass .";

?>

4

Output:

 http://localhost/rakesh/welcome.php



3. Example of $_REQUEST Function( )

The PHP also made one more function to transfer the information called $_REQUEST, which contains both $_GET method and $_POST method. $_REQUEST() function used to fetch the information from  both $_GET method and $_POST method. It is used when we are unsure which method is appropriate to use.

5


<html>

<head>

<title>php variables across pages</title>

</head>

<body>

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

Username : <input type="text" size="25" name="username" value="" />

Password : <input type="password" size="25" name="pass" value="" />

<input type="submit" name="sumbit" value="Submit me!" />

</form>

</body>

</html>


Welcome.php

<?php

6

$name = $_REQUEST['username'];

$pass = $_REQUEST['pass'];

7

echo "Your username is $name ."."<br/>";

8

echo "Your password is $pass .";

?>

9




Ads