Retired Documentation:  You are using the documentation for version 1.7.3 which was retired in 2013. Go here for the latest version documentation or check here for your available upgrades to the latest version.

Session Class

Calling the Session Class

ExpressionEngine uses the Session class for storing information about the user currently visiting the ExpressionEngine site. If the user is a member and is logged in, then their various preferences and privileges are loading into variables, which are then immediately available throughout the entire program without the use of a query. To use the Session class in your modules, simply call the $SESS global in every function that will require the use of any text.

function session_example()
{
    global $SESS;

    if ($SESS->userdata['group_id'] != 1)
    {
        exit('Not a Superadmin');
    }
    else
    {
        $admin_email = $SESS->userdata['email']
    }
}

The Functions class has numerous functions that use the information in the Session class variables to provide useful information for modules and plugins, such as allowed weblogs and access to areas. Make sure to check out the Functions class reference file for more information on these functions

User Data Information

The $SESS->userdata variable is an array that contains information about that specific user, and it will likely be the most used part of this class for any module. Below is a list of the variables that it contains. NOTE: If a user has a group_id of 1, then they can access anything, no matter the other settings.

On the Control Panel side of ExpressionEngine a few more variables are included:

Tracker Array

The Session class has one more useful variable that is only available on the user side of the site. $SESS->tracker is an array that contains the last five ExpressionEngine pages viewed by this user in the form of a ExpresionEngine query string (i.e. '/weblog/comments/' or 'index' for main site page). The array's keys ranges from 0-5.

$current_page = $SESS->tracker['0'];
$last_page = $SESS->tracker['1'];
$two_pages_ago = $SESS->tracker['2'];

If a page is constantly reloaded, ExpressionEngine will not allow the array to fill up with just the page's query string but waits until the user visits another page before updating the tracker array.

Top of Page