Use sessions to protect your web pages in PHP


Sessions are basically used for passing information from one page to another page.  When you are browsing a website with sessions you will get the a unique id that that can be in the form of a cookie or a query string.



Sessions are basically used for passing information from one page to another page.  When you are browsing a website with sessions you will get the a unique id that that can be in the form of a cookie or a query string.

To start a new session you have to use the
session_start() function in PHP This function is required on every page where you want to make use of session variables.

The next function is session_register(). This function registers one or more global variables with the current session. You can assign a variable to the session_register function as shown below.

session_register( "username" );

 

Now we will see a quick example how to create a sessions and passing value from one page to another page.

Add the following code into the first.php file.

<?php

session_start();

session_register( "username" );

session_register( "password" );

$username="deepak";

$password="shah";

?>

add the bellow code in the second.php file.

<?php

session_start();

?>

<html>

<body>


<?php

echo "Your username is: $username <br>";

echo "Your password is: $password ";

?>

</body>

</html>

Now visit the first.php file and then the open the second.php file.  When you browse the second.php file you will see the name and password values on the browser.

Your username is: terry

Your password is: moves

This way you can pass the pass the values in your web application.





Added on December 18, 2007 Comment
}