Home Tutorial Php Variable-across-pages PHP Variable Across Pages

 
 

Share on Google+Share on Google+
PHP Variable Across Pages
Posted on: January 9, 2010 at 12:00 AM
Advertisement
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'];

$pass = $_GET['pass'];

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

echo "Your password is $pass .";

?>

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>

<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

$name = $_POST['username'];

$pass = $_POST['pass'];

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

echo "Your password is $pass .";

?>

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.


<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

$name = $_REQUEST['username'];

$pass = $_REQUEST['pass'];

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

echo "Your password is $pass .";

?>




Advertisement

Related Tags for PHP Variable Across Pages:


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 9, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com