This is a brief introduction to PHP sessions. Creating sessions are very easy! Frameworks and CMSes usually have a built in class to handle PHP sessions, but today I was working on a project which didn't have such a class. Also this is a good article if you are just learning PHP. I wrote this article back in 2005.

Sessions allow you to set variables in one script and access them from another. To create a session you can use the Session_start function.

When working with sessions always place the following function on the first line of your script:

<?php session_start(); ?>

Variables

You can assign variables to a session by using the $_SESSION global variable.

$_SESSION['test'] = 'hello';
$value = $_SESSION['test'];
echo "The value of the session variable 'test' is ($value)";

Access saved data

The session_decode() function allows you to replace current session with encoded session data.

Code Igniter

As I now do most of my work in Code Igniter I'd like to mention that Code Igniter's session class does not utilize native PHP sessions. Read more about CodeIgniter's Session Class.