ExpressionEngine Docs

Session Class

Calling the Session Class

class Session

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. This class is initialized automatically.

function session_example()
{
    if (ee()->session->userdata('group_id') != 1)
    {
        exit('Not a Superadmin');
    }
    else
    {
        $admin_email = ee()->session->userdata('email')
    }
}

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

User Data Information

userdata($which[, $default = FALSE])

Parameter Type Description
$which String The key you’re looking for (see below)
$default Boolean An optional default value to use if the key isn’t set
Returns Mixed The value associated with the key

Note: We recommend using the Session::userdata method instead of accessing the userdata property directly.

The userdata method allows you to access information about the current 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:

$group_id = ee()->session->userdata('group_id');

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:

Flash Data for Redirects

set_flashdata($key[, $val = ''])

Parameter Type Description
$key Mixed The name you want to store the data under
$val String The message that goes along with the message type
Returns Void

You may sometimes need to store small pieces of data, such as language keys, across page requests to show as result messages. You can do this using redirect flash data.:

ee()->session->set_flashdata('result_message', 'Entry Deleted!');
ee()->functions->redirect(BASE.AMP.'C=addons_modules'.AMP.'M=show_module_cp'.AMP.'module=fortune');

// On the new page
$message = ee()->session->flashdata('result_message');

Flash data will often be used to specify a Control panel class $cp_message variable ('message_success', 'message_notice', 'message_error', 'message_failure') the key and the message as the value, as such default view variables are displayed automatically.

Note: Due to internal limitations this will only work in combination with the redirect method of the Functions class. Also keep in mind that this data is stored in a cookie, which have limited capacity.

Cache Access

The Session cache is provided for you to use for “flash” content, i.e. values that you would like to persist during a page load, helping you eliminate redundant queries and PHP processing.

Here is an example of how one might utilize the cache methods. This way, no matter how many times this method is called on a given page load (for instance, a tag being used twice on a template, or within a tag that might loop, such as a plugin within the Channel entries tag), the query and loading of the array occurs only once.

if ( ! ee()->session->cache('super_class', 'admins'))
{
    $query = ee()->db->select('member_id')->get('super_class_admins');

    if ($query->num_rows() > 0)
    {
        $cache = array();

        foreach ($query->result() as $row)
        {
            $cache[] = $row->member_id;
        }

        ee()->session->set_cache('super_class', 'admins', $cache);
    }
}

// set a local variable from the cached

You can see an example of real-world usage of Session::cache in the Channel module’s fetch_custom_channel_fields() and next_prev_entry() methods, and the IP to Nation module’s get_country() method.

set_cache($class, $key, $val)

Parameter Type Description
$class String Name of the class you want to store this under, only used as an identifier, does not need to be the same as the class you’re currently in
$key String Name of the cache item you’re storing
$val Mixed The data you want to store
Returns Object Session class

Set the value of a session cache item:

ee()->session->set_cache('module', 'name', $value);

cache($class, $key[, $default = FALSE])

Parameter Type Description
$class String Name of the class you want to retrieve from
$key String Name of the cache item you’re retrieving
$default Mixed Default value if the $key doesn’t exist
Returns Mixed The stored data

Retrieve the value of a Session cache item:

$value = ee()->session->cache('module', 'name');

Tracker Array

property $tracker

The Session class has one more useful variable that is only available on the user side of the site. ee()->session->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. ‘/channel/comments/‘ or ‘index’ for main site page). The array’s keys ranges from 0-5.

$current_page = ee()->session->tracker['0'];
$last_page = ee()->session->tracker['1'];
$two_pages_ago = ee()->session->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.