Reading and Writing Session Data in PHP

Working with session data in PHP is also simple. A user can store all the session data as keys and values in the $_SESSION[] superglobal array. So a user might store the user’s first name using:

$_SESSION[“firstName”] = “Aman”;

Then display the user’s first name in the same page request or during a later request

echo( $_SESSION[“firstName”] );

A user can store any type of data in sessions, including arrays and objects:

$userDetails = array( “firstName” => “Aman”, “lastName” => “Nagpal”, “age” =>34 );
$_SESSION[“userDetails”] = $userDetails;

Scroll to Top