Session


  • Sessions variable are used to identify a user which store on your server.

  • It is necessary to save state information so that information can be collected from several interactions between a browser and a server. Sessions provide such a mechanism.

  • A new session is created if one does not already exist.

Starting a PHP Session


You can store information about user in your PHP session.
Example :
<?php session_start(); ?>

<html>
<body>

</body>
</html>

Storing Session Variable


A simple way to store and retrieve session variables is to use the PHP $_SESSION variable.
Example :
<?php
session_start();
// store session data
$_SESSION['count']=15;
?>

<html>
<body>

<?php
//retrieve session data
echo "Pageviews=". $_SESSION['count'];
?>

</body>
</html>
Output :
Pageviews=15 

Destroying a Session


If you want to delete some session data,use the unset() or the session_destroy() function.
Example :
<?php
session_start();
// store session data
$_SESSION['count']=15;
?>

<html>
<body>

<?php
if(isset($_SESSION['count']))
  unset($_SESSION['count']);
  echo " Session Destroyed ";
?>

OR

<?php
session_destroy();
?>

</body>
</html>
Output :
Session Destroyed