PHP session tutorial

Using PHP session variable is a mechanism to store user’s data in server. And sessions will last till we are on the site, it will be destroyed when the user quit the site.
One main example of session usage is login information. When a user login to a site with his user name and password, sessions will be created for that user in server. By using these session variables servers and our PHP programs identify each user.

For each user session will create a unique ID , and other session variables and values are stored based on this ID.

How to start a PHP Session

For storing theWe need to start session before going to store any session variables and all. Function using for this is session_start(). It must be appear in our page before starting any output on the page. Better to use before our<html> tag.
Syntax for a PHP session start will be :
<?php session_start(); ?>

Storing a value in session
$_SESSION is the variable for session. We need to specify the the custom name for each session variable and value associated with that.
For example , if we want to store the user name and password of a user in PHP session, we can store like as below
$_SESSION[‘user_name’]=$username;
$_SESSION[‘password’]=$password;

Then user name will stored in the session variable $_SESSION[‘user_name’] and password will be stored in the session variable $_SESSION[‘password’]. And we can retrieve these values in any other pages for that domain as long as that user stay on that site.
See the sample program to create and retrieve the PHP session variables

<?php php session_start();?>
<html>
<?php
$username="syam";
$password="mysql";
$_SESSION['user_name']=$username;
$_SESSION['password']=$password;
?>
<body>
<?php
echo "Hi ".$_SESSION['user_name']. " Your password is ".$_SESSION['password'];
?>
</body>
</html>

Output of this program will be :
Hi syam Your password is mysql

Its better to check session variable is stored or not before retrieving its value for any operation to avoid any errors or other issues.
See the sample program with checking session exist or not for counting the page views of a visitor

<?php php session_start();?>
<html>
<?php
$username="syam";
$_SESSION['user_name']=$username;
if(isset($_SESSION['page_views']))
{
$_SESSION['page_views']=$_SESSION['page_views']+1;
}
else
{
$_SESSION['password']=1;
}
?>
<body>
<?php
echo "Hi ".$_SESSION['user_name']. " You have visited ".$_SESSION['page_views']." pages";
?>
</body>
</html>

In this example output will be :
Hi syam You have visited 1 pages
OR
Hi syam You have visited 2 pages
etc.. for each time he visit the page with this code his session value for page visit count will be increment by one. If its his first visit our session exist check will find that session is not existing and will assign the value 1, otherwise it will increment.

PHP Session Destroy

If we want to destroy the session, there are two functions are for this

  • unset()
  • session_destroy()

Difference between these two are

  • Using unset(0 function we can destroy one particular session variable for that user
  • using session_destroy() we can destroy all the sessions for that user

unset($_SESSION[‘username’]) will destroy only the session variable used for storing the username of that user. If we used session_destroy() function all the session variables including $_SESSION[‘username’] will be destroyed.

There are several other functions used with PHP session , we will discuss about those in our later posts.

Share
Share
Share