PHP Sessions (original) (raw)

Summary: In this tutorial, you will learn how to use PHP sessions to preserve the state of the web application across pages during a session.

Introduction to PHP sessions #

The HTTP protocol is stateless. For example, when you visit the product page product.php, the web server responds with the page:

Suppose, you click the add to cart button on the product.php page and navigate to the cart.php page, the web server won’t know that you have added the product to the cart.

To persist the information across the pages, the web server uses sessions. In this example, when you click the add to cart button, the web server will store the product on the server.

When you view the cart.php page, the web server gets the products from the session and displays them on the cart.php page:

How it works.

Sessions allow you to store data on the web server associated with a session id. Once you create a session, PHP sends a cookie that contains the session id to the web browser. In the subsequent requests, the web browser sends the session id cookie back to the web server so that PHP can retrieve the data based on the session id.

Creating a new session #

To create a new session, you call the session_start() function:

`<?php

session_start();`Code language: PHP (php)

When the session_start() runs at the first time, PHP generates a unique session id and passes it to the web browser in the form of a cookie named PHPSESSID.

If a session already exists, PHP checks the PHPSESSID cookie sent by the browser, the session_start() function will resume the existing session instead of creating a new one.

Since PHP sends the PHPSESSID cookie in the header of the HTTP response, you need to call the session_start() function before any statement that outputs the content to the web browser.

Otherwise, you will get a warning message saying the header cannot be modified because it is already sent. This is a well-known error message in PHP.

Where PHP stores session data #

PHP stores session data in temporary files on the web server by default. You can find the location of the temporary files using the directive session.save_path in the PHP configuration file.

The ini_get() function returns the value of the session.save_path directive:

`<?php

echo ini_get('session.save_path');`Code language: PHP (php)

Or you can call the session_save_path() function:

`<?php

echo session_save_path();`Code language: PHP (php)

Typically, the session data is stored in the /tmp folder of the web server e.g, /xampp/tmp .

Accessing session data #

Unlike cookies, you can store any data in the session. To store data in the session, you set the key and value in the $_SESSION superglobal array.

For example, in the index.php file, you store the user string and roles array in the session as follows:

`

PHP Session Demo Go to profile page `Code language: PHP (php)

How it works:

The index.php displays a link that navigates to the profile.php page. In the profile.php file, you can access session data as follows:

`

<p>Welcome <?= $_SESSION['user'] ?></p>
<p>Current roles:</p>
<ul>
    <?php foreach ($_SESSION['roles'] as $role): ?>
        <li><?= $role ?></li>
    <?php endforeach; ?>
</ul>

`Code language: PHP (php)

How it works.

Deleting the session data #

Whenever you close the web browser, PHP automatically deletes the session. Sometimes, you want to explicitly delete a session, e.g., when you click the logout link. In this case, you can use the session_destroy() function:

`<?php

session_destroy();`Code language: PHP (php)

This session_destroy() deletes all data associated with the current session. However, it does not unset data in the $_SESSION array and cookie.

To completely destroy the session data, you need to unset the variable in $_SESSION array and remove the PHPSESSID cookie like this:

`<?php session_start();

// remove cookie if(isset($_COOKIE[session_name()])){ setcookie(session_name(),'',time() - 3600, '/'); }

// unset data in $_SESSION $_SESSION[] = array();

// destroy the session session_destroy();`Code language: PHP (php)

Notice that we used the session_name() function to get the cookie name instead of using the PHPSESSID. PHP allows you to work with multiple sessions with different names on the same script.

Summary #

Did you find this tutorial useful?