What is a Session in PHP? - PHP Session ,Variables


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.
The session is the time period of user is working on the website, this time duration and user details are stored in the server. For this we can use the super global $_SESSION as variable.

Session allows data to be transfer from one page to another.
Session information is temporary and information valid until the user is using the website.
A Session assigns a unique ID, UID to each visitors.
eg:
Session Start » <?php session_start(); ?>

Example for Session Creating Variable,

<?php
session_start();

if(isset($_SESSION['user']))
$_SESSION['user']=$_SESSION['user']+1;
else
$_SESSION['user']=1;
echo "user=". $_SESSION['user'];
?>

This shows,
user = 1 // which is the user views count is 1.

Destroying Session:

To delete the session value, we can use the unset() or the session_destroy() function.

The <strong>unset()</strong> function is used to free the specified session variable:
<?php
session_start();
if(isset($_SESSION['user']))
  unset($_SESSION['user']);
?>

where as <strong>session_destroy()</strong>, destroy the session, which resets the session value.
Get Related News: , ,

0 comments:

Thanks for taking time to leave a comment