PHP Session

PHP session has the ability to store information on the server that can be accessed across multiple PHP pages.

In PHP, There are three common methods to pass data from any page to other pages. The Session is one of them. The second method is using Cookies and the third method is PHP Form. However, forms can only pass data between two pages only.


Use of PHP Sessions:

A PHP session is used when you need to store user information and need to access that information on various pages. For example, you save user profile, user preferences, shopping item that can be used later such as on payment page.

This session data is stored temporarily and deleted after a limited time. By default most server has session time out after 24 minutes (session.gc_maxlifetime = 1440) but it can be changed using php.ini file.


Where PHP Session store data:

PHP session store its data in a temporary folder on the server. You can find this temporary folder location inside ‘php.ini’ file configuration using ‘session.save_path’ value.

For example, local/XAMPP ‘session.save_path’ is set as ‘/Applications/XAMPP/xamppfiles/temp/’.

When you start a session:

  • PHP first creates a unique identification string made of 32 hexadecimal numbers.
  • Then, a file is created on the server with the prefix ‘sess_’ and unique identifier for example ‘sess_4a7e9f06243a550c72ca84d28c0610d8’.
  • Then a cookie PHPSESSID is created on a user’s browser to store the same unique session id 4a7e9f06243a550c72ca84d28c0610d8.
  • Now, these two things are used by PHP Session to store data on a server and passing this data to user’s PHP script whenever required.

Start a PHP Session

session_start() function is used to start PHP Session. Once session stared, you can set Session variables using PHP global variable: $_SESSION.

<?php

// Start the session
session_start();

// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";

// Display session variables
echo "Session variables are set.".$_SESSION["favcolor"].$_SESSION["favanimal"];

?>

Run : http://ideone.com/o6pvv9

Now you can access these session variables in any PHP page.

<?php

// Start the session
session_start();

// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";

// Display session variables
echo "Session variables are set.".$_SESSION["favcolor"].$_SESSION["favanimal"];

?>
Note: Note: You need to start the session at the beginning of each page where you want to access the session variables.

Run : http://ideone.com/Oyit6V


Modify a PHP Session Variable

To change a value of any session variable, just overwrite PHP session variable’s value.

<?php

// Start the session
session_start();

// Set session variables
$_SESSION["favcolor"] = "balck";
$_SESSION["favanimal"] = "dog";

echo "Session variables are set.".$_SESSION["favcolor"].$_SESSION["favanimal"];

?>

Run : http://ideone.com/RHh3P2


Destroy a PHP Session

Before deleting session data, we need to start the session environment using the session_start() function.

We can remove global session variables and destroy all session data using session_unset() and session_destroy()

If you need to delete only a single session variable, you can use the unset() function. See example below:

<?php

// Start the session
session_start();

// Display session data before deletion
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.".$_SESSION["favcolor"].$_SESSION["favanimal"];

// Destroy session data
// unset($_SESSION["favanimal"]); 
session_destroy();

// Display session data after deletion
echo "Session variables are set.".$_SESSION["favcolor"].$_SESSION["favanimal"];

?>

Run : http://ideone.com/GNxJ6a


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
Difference Between Cookies and Session