Home Free Templets Works Mind Chiller Photo Gallery Family & Friends Links
 
 

Using Sessions in PHP

 Why sessions :

It is said that HTTP (Hyper Tex Transffer Protocol )tis stateless protocol , which means, information entered by user in one HTML page is not available to other pages.

Let us see what does that mean , suppose there is a page named "start.php" where you can enter your name and store it in variable "$name". And after that in all susequent pages if you print variable "$name" then you will find it "$name" variable does not contain your name. That means if you initialize a variable in one page , that value will not be available to other pages.

Thus we need some special variables that can be set in one page and the same variable will be shared in all the subsequent pages of that website. These special variables are called "session variables" and written as $_SESSION['varname'] in php.

A Practical Example

Suppose you have your mail account in "yahoo.com". So, when you logg in to your mail account , you have to give your "Username" and "password" at the "login" page. And once your username is verified you enter into your mail account. After that you can goto any page without reentering your "username" and "password" again. So, it is clear that , the "username" and "password" in all the subsequent pages, otherwise you had to enter "username/password" to each pages.

A code example

Copy the following code to a file named "sess1.php". Run the program in your browser, press the "Refresh" button repeatedly. You will see that each time you press the "Refresh" button the number displayed on the page is incremented by 1.


<?php
session_start();
?>
<?php
if (!isset($_SESSION['count']))
$_SESSION['count']=0;
$_SESSION['count']++;
echo $_SESSION['count'];
?>

Explanation of the above code :

If you want to use sessions in your code then you must write the statement "session_start()" at the very first line of the code .
Beware : if you are using Internet Explorer as browser it is very likely that you encounter an error " headers already sent". This problem is discussed in more deatils in the tutorial "headers already sent"

Now, here we have created a session variable named "count". This session variable "count" is accessed by the statement "$_SESSION['count']". Now at first we are checking if the "$_SESSION['count']" variable has been initialised or not. If it is not initialised then we give it a value of 0. Afterwards when press the refresh button the page will be reloaded , but the "$_SESSION['count']" will remember its last value when the page was loaded last time. Then it will add 1 to the last value and update itself. If you do the same thing with a nornal variable you will see that everytime the value dispalyed is 1, as it cannot remember the value of the last time it was loaded.

So, If you ever need to remember the value of any varible , last time you opened the page , then you may use session variables to do that.

So that's the basic part of handling sessions. Hope you've enjoyed it . Thank You.

Back to Tutorial Index page

Your comments are most welcome admin@koderguru.com