Docs: Session Variables

You need to be logged in to edit this page.

Session variables can be used in any of your extension files. The easiest way to view is to print_r the SESSION array in your footer.php file:

<?
print_r($_SESSION);
?>

or do a foreach loop:

<?
foreach($_SESSION as $name => $value) {
       print ("$name: $value<br />";
}
?>

You can also do this for cookies:

<?
foreach($_COOKIE as $name => $value) {
       print ("$name: $value<br />";
}
?>

Here are a few of the standard session variables:

$_SESSION['user_id']
$_SESSION['first_name']
$_SESSION['logged_in'] and $_USER['logged_out']
$_SESSION['order'] or $_USER[cart] ('empty' or 'full')
$_SESSION['country_id']
$_SESSION['country']
$_SESSION['mode']
$_SESSION['lang']
$_SESSION['browser']
$_SESSION['platform']
$_SESSION['screen_resolution']
$_SESSION['page_views']

Example 1: check if user is logged in using the user_id session var

In this example we check if the visitor is logged in with the user_id session var and use the custom_text page variable to display text on the cart page:

<?
if(isset($_SESSION['user_id']) and $_PAGE['page']=='order'){
      $_PAGE['custom_text'] = "<p>Welcome back ".$_SESSION['first_name'].".</p>";
}
?>

The $_PAGE['custom_text'] variable can be inserted into the top.inc file and will be displayed immediately after the h1 tag.

Example 2: display a message using a snippet

Here is another example using session variables to create a snippet to insert into your template:

if(isset($_SESSION['first_name']) and isset($_SESSION['user_id'])){
      $snippet1 = "<p>Welcome back $_SESSION[first_name].</p>";
} else {
      $snippet1 = "<p>Welcome guest. <a href="/login/">Log in</a> or <a href="/account/new">create an account</a>.</p>";
}

 

Revisions history (52)    ( hide | show )     Variables > Session Variables