PHP Session

A PHP session variable is used to store the information about a user and the information is available in all the pages of one application.

PHP Session

PHP Session

     

 A PHP session variable is used to store the information about a user and the information is available in all the pages of one application.

Session is much like the conversation over telephone, whenever we start talking to someone, session starts, and after disconnecting  the line session expires. Our computer knows its user and it may keep track of who logs-in, time duration etc. , but in the case of web servers, it can not keep the tracks of all these things. That's why we need session. Session is capable of keep the record of the user name, time duration etc.

Session does not store the data permanently, whenever the user close the website every data is lost. Session creates an UID(Unique ID), which is unique for every user. The UID is generally store in the cookie or it displays in the URL.

 

Example 1:

  <?php

SESSION_start();

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

  $_SESSION['view']=$_SESSION['view']+1;}

else

  $_SESSION['view']=1;

echo "Views=".$_SESSION['view'];

?>

Output(after refreshing 9 times the output will be): Inside if Views=10

Example 2 (To close the session):

<?php

SESSION_start();

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

  $_SESSION['view']=$_SESSION['view']+1;}

else

$_SESSION['view']=1;

echo "Views=".$_SESSION['view']."<br/>";

unset($_SESSION ['view']);

echo "Views=".$_SESSION['view'];

?>

Output: Views=1 Notice: Undefined index: view in C:\wamp\www\phpBasics\sessionPHP.php on line 12 Views= Example 3:

<?php

session_start();

echo"<b>Few Important Session functions are as follows:</b>"."<br/>"; 0

echo"<b>Session Id is:</b>".session_id()."<br/>";

echo"<b>Session module name is:</b>".session_module_name()."<br/>";

echo"<b>Session path is:</b>".session_save_path()."<br/>"; 1

echo"<b>Session name is:</b>".session_name()."<br/>";

echo"<b>Session Id is:</b>".session_id()."<br/>";

?> 2 Output:

Few Important Session functions are as follows:
Session Id is:rod2d974k7obqfa15bib5hikv4
Session module name is:files
Session path is:c:/wamp/tmp
Session name is:PHPSESSID
Session Id is:rod2d974k7obqfa15bib5hikv4