PHP Session

A session is a frame of communication between two mediums. PHP sessions are used to temporarily store and transmit information from one page to another (until the user close the website).

Problem:  The HTTP address does not keep state, So the web server has no idea who you are or what you do.

Solution: Session variables address this issue by saving user data that can be used across several pages.

PHP session generates a unique user id for each browser in order to identify the user and avoid conflicts between browsers. The PHP engine generates the session IDs at random. Because the session data is maintained on the server, it is not required to be provided with each browser request.

Note: Session variables are maintained until the user closes the browser. If you require long-term storage, then consider storing the data in a database.

Use of PHP Session: The PHP session technique is extensively used in shopping websites where we need to save and send cart information from one page to another, such as username, product code, product name, and product price.

PHP Session Steps

The following are the steps involved in PHP sessions:

  • Start PHP Session
  • Store Session Data
  • Access Session Data

You can also Modify and Delete a session.

Start a PHP Session

The first thing you should do is start a session. Session variables can be created after a session has begun to store data. To start a new session, use the PHP session start() method. It also gives the user a new session ID.

The following piece of code is used to start a new PHP Session.


<?php
  
session_start();
  
?>

Note: The session_start() function must be written prior to any HTML tags and should be first written at start of page.

Storing Session Data

The $_SESSION[] superglobal array stores session data in key-value pairs. During the lifetime of a session, the stored data can be retrieved.

The PHP code for storing a session with two session variables “Name” and “Age” is shown below.


<?php
  

  
$_SESSION["Name"] = "Henry";
$_SESSION["Age"] = "21";
  
?>

Let’s have a look at an example by making a new page named “Storing_Session.php.” We will create a new PHP session and set some session variables on this page:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["Name"] = "Henry";
$_SESSION["Age"] = "21";
echo "Session variables are set.";
?>

</body>
</html>

Accessing Session Data: 

Data saved in sessions can be accessed quickly by first calling session start() and then sending the corresponding key to the associative array $_SESSION.

Now, Let’s create a new page called “Access_Session.php.” We will retrieve the session information we set on the first page (“Storing_Session.php”) from this page.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["Name"] . ".<br>";
echo "Age is " . $_SESSION["Age"] . ".";
?>

</body>
</html>

Note: The global $_SESSION variable stores all session variable values.

Now, Lets see examples below to modify and delete a session:

Modify a PHP Session Variable

Simply overwrite a session variable to make changes:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// to change a session variable, just overwrite it
$_SESSION["Age"] = "22";
print_r($_SESSION);
?>

</body>
</html>

Destroying Certain Session Data:

The unset feature can be used with the relevant session variable in the $_SESSION associative array to erase only that session’s data.

Lets unset Session Age:


<?php
  
session_start();
   
if(isset($_SESSION["Name"])){
    unset($_SESSION["Age"]);
}
  
?>

Destroying Complete Session: 

Use session unset() and session destroy() to delete all global session variables and destruct the session. These functions do not require any argument.

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php

// It is used to remove all session variables
session_unset();

// destroy the session
session_destroy();
echo "Session Destroyed "
?>

</body>
</html>