PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: unset
unset - phponwebsites.com
Showing posts with label unset. Show all posts

22 May 2014

PHP - Session

                      Session is the temporary memory to store a particular value. All of you use gmail, that gmail is the bset example for session concept. If you login gmail, then close the browser. Now you open gmail in your browser, you login gmail again. Unlike, you logout from gmail and close the browser. Now you can't login again. It comes to login page. This is the session concept. The value is stored in session until you destroy the session.

Session in php:


                      In php, there is 3 steps for session.
                      1. Start the session using session_start().
                      2. Load values to session variable by $_SESSION['name']='Guru'.
                      3. Destroy the session by session_destroy() or unset($_SESSION['name']).

session_start() in php:


                      It is used to start the session in php. Consider folowing example in php:

<?php    
         session_start();
         $_SESSION['name']='Guru';    
         echo $_SESSION['name'];
?>

                     Now you'll get output:     Guru

Suppose you run file without session_start(). Then you'll get error message like below:

session_start() in php

session_destroy() in php: 


                   It is used to destroy the session. Consider following example in php:
welcome.php:

<?php    
         session_start();
         $_SESSION['name']='Guru';    
         echo $_SESSION['name'].'<br>';
         echo'<a href="signout.php">Signout</a>';
?>

signout.php:

<?php    
         session_start();
         session_destroy();
         header('location:welcome.php');
?>

                When you run the welcome.php, your output like this:

                       Guru
                       Signout

                  When you click the signout, it destroy the session and navigates to welcome.php. Now you try to clikc back button on your browser, it go to welcome.php page, but the value stored in session is not displayed. It give error message like below:

session_destroy() in php

unset() in php:


                  You can also delete the session using unset() in php. The script for unset() as follows as:

<?php    
         session_start();
         unset($_SESSION['name']);
         header('location:welcome.php');
?>

It is also give same output like session_destroy() in php.

Related Post: