Home > DeveloperSection > Beginner > PHP Session

PHP Session


PHP PHP 
Ratings:
1 Comment(s)
 3589  View(s)
Rate this:

PHP Session

Session management is a mechanism to maintain state about a series of requests from the same user across some period of time. That is, the term "session" refers to the time that a user is at a particular web site. The problem is that HTTP has no mechanism to maintain state. Individual requests aren't related to each other. The Web server can't easily distinguish between single users and doesn't know about user sessions. Session management refers to the way that associate data with a user during a visit to a Web page.

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application. I.e. A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the user’s computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.

In this article I will be described, how to start session, store value in session variable and how to destroy session in PHP.

Start Session:

Before you can store user information in your PHP session, you must first start up the session. session_start() function is used for start session into PHP page.

Syntax:

?php

// Start session 

session_start();

?>

<html>

    <head>

        <title></title>

    </head>

    <body>

       

    </body>

   

</html>

Example:

Let’s we have an example for showing the page view counter at every visiting of user in that page.

<?php

// Start Session

session_start();

// check session value has been set or not

if(isset ($_SESSION['PageViewUser']))

{

    // incrementcounter by 1 if session value is already set

    $_SESSION['PageViewUser'] = $_SESSION['PageViewUser']+ 1 ;

   

}

else

{

    // if session value not set, then value set by 1 in session variable

    $_SESSION['PageViewUser'] = $_SESSION['PageViewUser']+1 ;

   

}

?>

 

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PHP Session</title>

    </head>

    <body>

        <div id ="viewContainer">

            <h1> This is session page testing </h1>

        <?php

        // dispaly number ofvisitor in  page

           echo 'Total Visitor in current page :  '.$_SESSION['PageViewUser'];

         

        ?>

       </div>

    </body>

</html>

 

Output:

PHP Session

Here, page viewed two times so total visitor in current page is displayed 2.

Destroying Session:

There are basically two functions which is for destroying Session, first one is unset() function and second one is session_destroy() .

The unset() function is used to free specified session variable.

Example:

 <?php

// Start Session

session_start();

// check session value has been set or not

$_SESSION['TestUnset'] ="Unset";

?>

 

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PHP Session</title>

    </head>

    <body>

        <div id ="viewContainer">

            <h1> This is session page testing </h1>

        <?php

        // dispaly number ofvisitor in  page

           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";

           unset ($_SESSION['TestUnset']);

           echo 'Session Destroyed'."</br>";

           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];

        ?>

       </div>

    </body>

</html>

 

Output:

PHP Session

You can also completely destroy the session by calling the session_destroy() function:

Example:

<?php

// Start Session

session_start();

// check session value has been set or not

$_SESSION['TestUnset'] ="Unset";

?>

 

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

        <title>PHP Session</title>

    </head>

    <body>

        <div id ="viewContainer">

            <h1> This is session page testing </h1>

        <?php

        // dispaly number ofvisitor in  page

           echo 'Session Value is :  '.$_SESSION['TestUnset']."</br>";

           session_destroy() ;

           echo 'Session Destroyed'."</br>";

           echo 'Now Session Value is :  '.$_SESSION['TestUnset'];

        ?>

       </div>

    </body>

</html>

Output:

PHP Session


Very Nice post on Php Session management

By Harish Pal on   one year ago

Hi Arun,

Excellent Blog post Php Session management. I have encountered similar blog post on PHP Session at the this link: http://bit.ly/1Fw5AC2  at some Php questions and answers forum.


Don't want to miss updates? Please click the below button!

Follow MindStick