Monday 12 March 2012

Session


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.

To create session two step are there as shown below.

  • Start the session.

To start the session session_start() function is used.

  • Set the value of the session.

To set the session value the global variable $_SESSION[‘session_name’] is used.

Syntax for setting the value

$_SESSION[‘session_name’] = value;

Example

<?php     
$user="Hemangi";
     $pass="123";
     if($user == "Hemangi")
     {
              session_start();
              $_SESSION['user_name']="Hemangi";
              header("Location: login.php");
     }
?>

Example – login.php

<?php
          session_start();
          echo "Wel - Come".$_SESSION['user_name'];
?>
<a href="logout_ex.php">Logout</a>

To destroy the session there are 3 step to be used.

  • Start the session
  • Clear the session – to clear the session the function unset is used.
  • Destroy the session – to destroy the session the function session_destroy() is to be used.

Example – logout.php

<?php
          session_start();
          unset($_SESSION['user_name']);
          session_destroy();
          echo "Session Destroy successfully";
          echo $_SESSION['user_name'];
?>

Posted By : Hemangi Zala

No comments:

Post a Comment