PHP Sessions

Imagine you make a hotel reservation and are given a reservation number. The first thing you do when you arrive is check in using your reservation number. Once at the hotel, you can go to your room, visit the pool, or sit at the bar. No matter what you do at the hotel, you are doing all of these things while checked in under your reservation number.

A PHP session is like your reservation number. It stores information that is gathered for a single user and can be used no matter where the user is directed within your script. No matter where the user is taken, we know who it is. We know when the user arrives and when the user leaves. Once the user leaves, all the information stored in that session is deleted.

When To Use Sessions

Sessions can be used for a variety of different reasons, but one time a session can be used is with an online shopping cart. Once someone visits your site, they can add items to their shopping cart, change quantities, and remove items from their cart. However, once they leave your site, all the items in their shopping cart are removed and the session ends.

How To Use Sessions

If you want to use a session, you must first begin the session. As with the illustration of the hotel, this is done when you check in. Checking in to the hotel is the first thing you do when you arrive. Likewise, starting a session is done at the very beginning of the page.

To start a session, you use the following session variable:

session_start();

This will start a session with the user, assigning them a unique identification number (UID) for that session.

Storing Information In A Session

When you store information using a session, you can do so using an associative array. Once you have started the session, you can use the following code:

$_SESSION['views']=1;
echo “Pageviews:”. $_SESSION['views'];

To start, this bit of code would display:

Pageviews: 1

The above code is obviously used to display the number of pageviews from a particular user.

If the user has not viewed any pages before now, it will display 1 pageview.

For subsequent pageviews, we use the isset function.

The isset function will check to see if the user has viewed any pages thus far. If not, we create the “views” variable as shown above and start it off with 1 pageview.

If the user has already begun a session, we can retrieve the number of pageviews in this session and increment it by 1.

session_start();
if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1;
else $_SESSION['views']=1;
echo “Pageviews:”. $_SESSION['views']

If this is the third pageview from the same user, this bit of code would display:

Pageviews: 3

We first began a session for this user. We used if/else to say if the user has already begun a session, add 1 pageview to their current amount of pageviews. Else (if the user has not already begun a session), begin a new session starting with 1 view.

Ending A Session

When you are finished using the information stored in the session, you can destroy it by using the session_destroy() function:

session_destroy();

Once the session has been destroyed, all the data stored in the session will be lost.