Session – session basically a particular interval of time.

Session support in PHP consists of a way to preserve certain data across subsequent accesses.

A visitor accessing your web site is assigned a unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL.

The session support allows you to store data between requests in the $_SESSION superglobal array. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start()) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated.

  • Why session required?

Ans – Some importance of the session is penned below.

  • Like $_COOKIE, we have $_SESSION, which is a superglobal variable and stored over the server.
  • If the user’s browser does not support the cookies, we can use the session. Each session has a unique id assigned to it as per the user visit on the website.
  • In terms of storing data, the session stores more data than a cookie can store.
  • The session is used to carry information from one page to another.
  • The session can be used to get the count of visitors to the website.

Note: in any browser (ex. chrome,edge,firefox,safari,opera) session will be valid once it’s created. But if you create session variable under ‘igninto mode/private tab’ on browser, then after closing private tabs sessions also will vanished. That’s why many users open their accounts in ‘igninto mode’ into anoher PC, laptop.

For doing, session operation, we should start session. using session_start();

Let’s deep drive into that …

  • Let’s create a new session in PHP script.

File – code1.php

<?php
session_start();
 
$_SESSION[‘name’]=’shimanta das’;
echo ‘session started!’;
$_SESSION[‘loggedin’]=’active’;
echo “<br> <font size=’20px’ color=’blue’>login start!</font>”;
?>
output

  • You can continue session accross the PHP pages you can print session-id using session_id() method.

File – code2.php

<?php
session_start();
// print session variable ‘name’ value
echo $_SESSION[‘name’];
// assigning session variable ‘loggedin’ value intp variable ‘islogin’
$islogin = $_SESSION[‘loggedin’];
if(isset($islogin)){
    echo “<br> user logged in!! welcome user!! session active!!”;
}
echo “<br> session id: “.session_id();
?>
output

Let’s inspect chrome’s systems…

  • Let’s see how to modify session values.

File – code3.php

<?php
session_start();
 
echo ‘name is: ‘.$_SESSION[‘name’];
 
$_SESSION[‘name’]=’pritha saha’;
echo ‘<br>name is: ‘.$_SESSION[‘name’];
?>
output

Note: once vale of session variable changed it will make affect globally, not only that page.

  • Let’s see, how to destroy a PHP session.

For this we use ->

// remove all session variables
session_unset();
 
// destroy the session
session_destroy();

Let’s see implementation of this below…

File – code4.php

<?php
session_start();
 
echo $_SESSION[‘loggedin’];
session_unset();
 
session_destroy();
echo “<br>”;
echo $_SESSION[‘loggedin’];
?>
output