October 29, 2026

PHP – Sessions

In this chapter, we would be learning about PHP Sessions. This is a way to to make some data available across multiple pages. Similar to cookies, sessions use session variables to store data.

  1. Overview of Session
  2. Starting a Session
  3. Session Without Cookies

 

1. Overview of PHP Sessions

A file is created in the temporary direction by a session. This file is used to store registered session variables and their values. So during a visit, this stored data would be available accross the pages during that session(or visit). The location of the temporary directory  is set in the php.ini file by the session.save_path. This variable must be set before you can use session variables.

Three thing when a session is being setup:

  • a unique identifier is created for that session. This is simply a random hexadecimal number of 32 characters
  • PHP sends a cookie called PHPSESSID  to the the user’s computer to store the session id
  • a file is also created on the server in the temporary directory with the name of the session id prefixed by “sess_”

When a PHP script wants to get the value of a session variable, it retrieves the session identifier from the PHPSESSID cookie. Next it looks at the temporary directory and obtains the corresponding file. The it compares the value in the file with the value in the PHPSESSID cookie for validation.

A session is terminated when the user closes the browser or leaves the site. Sometimes a session is terminated after a period of inactivity (30 minutes for example)

 

2. Starting a Session

You can start a session in PHP by calling the session_start() inbuilt function. It first checks if there’s an already started session, and if not, it starts one. It is better to put this function call at the beginning of the page.

Let’s consider a program that starts a session, sets a session variable and increments the session variable each time a page is visited. The isset() function  is used to check if a session variable exists.

<?php
    //Start the session
    session_start();
    //check is the session variable is set
    if(isset($_SESSION["visits"])) {
        $_SESSION["visits"] += 1;
    }else {
        $_SESSION["visits"] = 1;
    }
    
?>
<html>
<body>
    <?php 
        echo "You have visited ". $_SESSION["visits"]. "times" ;
        echo "<br> Session Id is ". $_COOKIE["PHPSESSID"];
    ?>
</body>
</html>

 

Destroying a Session

To destroy a PHP session, you use the session_destroy() function. This function does not take any arguments and once called, can be used to destroy all session variables. However, to destroy just a single session variable, you can use the unset() function passing the session variable as parameter

About Auto Session

When auto session is set, then you don’t need to explicitly call the session_start() function to start.  This is done in the php.ini file by setting the  session.auto_start variable to 1.

 

3. Sessions Without Cookies

There are sometimes when when the client computer does not allow cookies. In situation like this, you can still send session ID to the browser anyways, using a different approach.

To achieve this, you need to define a constant SID once the session starts. It takes the form of session_name = session_id. But if theres is not session_id existing in the client, then it would be an empty string.

The code below shows how to pass the session id from one page to another called page2.php

<a  href = "page2.php?<?php echo htmlspecialchars(SID); ?>">