Session Variables Plugin

*** Update: now compatible with EE2.0

This is an Expression Engine plugin that allows you to store variables in PHP's native session.

Support can be found in the comments and in this EE forum thread.

Download Session Variables Plugin
Current Version: 1.1 (released 19 February 2010)

* Works with ExpressionEngine 2.0 and ExpressionEngine 1.6

1. Concept

Sometimes you may want to temporarily store a variable for a short amount of time without writing a value to the database. Expression Engine's sessions do not allow this, however PHP's native sessions do.

This plugin allows you to use PHP's native session variables.

Find out more about PHP Sessions.

2. How It Works

The session variables plugin works by using PHP's native session to store variables on the server. The variables can easily be set, retrieved and deleted.

Here are some examples of how to use it:

// setting the language variable to french
			
{exp:session_variables:set name="language" value="french"}  
				
// setting the language variable to the second segment
			
{exp:session_variables:set name="language" value="{segment_2}"}  
				
// setting the language variable to the posted value
			
{exp:session_variables:set name="language" value="POSTED_VALUE"}  
				
// getting the language variable

You are currently viewing this page in {exp:session_variables:get name="language"}  
				
// deleting the language variable
			
{exp:session_variables:delete name="language"}  
				

When using the set function, the name parameter and value parameter are required.

POSTED_VALUE is a constant that specifies the value of the posted variable with the specified name (in the example above it would refer to $_POST['language']).

When using the get function, only the name parameter is required.

When using the delete function, only the name parameter is required.

Note: The lifetime of the session is set in the php.ini file (session.gc_maxlifetime) and is 1440 seconds by default. It can be changed using the ini_set function, however it can be reset in unpredictable ways. This article explains how and why.

3. Installation

Download and unzip session_variables.zip, then follow the steps below.

For EE2.0:

  1. Open the session_variables_2_0 folder
  2. Upload the entire session_variables folder to the system/expressionengine/third_party/ folder

For EE1.6:

  1. Open the session_variables_1_6 folder
  2. Upload pi.session_variables.php to the system/plugins folder

4. Download

You can download and use the plugin for free here:

If this plugin has helped you and you are feeling generous then feel free to make a small donation, it is much appreciated:

5. Changelog

Version 1.1
  • Converted to an EE 2.0 compatible plugin (both versions available in download)
Version 1.0
  • Initial release

6. Comments

Blank Avatar
Julien
23 April 2008


Hello,

I just want to say a big thank you as I have been stuck with sessions in EE for quite a time. Your plugin was very helpful to me, thank you !

However, I just noted a small addition you might consider for your plugin: it could be good to be able to parse in a loop all session variables that have been entered, like a foreach loop in php.
Other than that it's perfect !

Blank Avatar
Julien
23 April 2008


I found small "bugs" however:
- the help you give in the plugin panel of EE is misleading; it tells to use {exp:session:delete name="language"} but the real syntax would be the one you gave in this post, that is session_variables instead of session !
- your plugin starts a php session even if it is already started; I've searched the php doc, and it says that the test if (session_id() == "") is not good to know whether a session exists or not.
In fact I found no solution for this, so I removed the block of your plugin that tests the existence of the session and put at the very top of my EE header a simple (with php on of course).
This is perhaps not the cleanest solution but it always works.

Regards

Blank Avatar
Ben
23 April 2008


hi julien, i'm glad you are finding the plugin useful. thanks for the note on the syntax, i'll change in next release.

the plugin uses the session_id() function so that a php session is not started if one already exists. i too have looked for a better solution and found none. apparently none exists (http://bugs.php.net/bug.php?id=16349&edit=1).

Blank Avatar
Jim Pannell
31 July 2008


Hi Ben

Thanks for the plugin - it looks great and has got me pretty excited! I'm just wondering how I would use this if I wanted to allow a user to click a link to set a session variable. Is this possible?

Cheers

Jim

Blank Avatar
Michael
24 September 2008


Hey, cool plugin. I'm not using it at the moment, but I stumbled across it looking for a solution to another issue.

You can probably do this as a better session check:


if(isset($_SESSION))
{

}

Blank Avatar
Mark
18 November 2008


I keep getting:

Session Plugin: name and value parameters must be entered

Help. What am I doing wrong?

Blank Avatar
Ben
18 November 2008


hi mark, you need to enter the name and value parameters when setting a session variable, so for example:

{exp:session_variables:set name="language" value="english"}

Blank Avatar
Jeff
19 November 2008


I'm having a heck of a time setting a session variable from a posted variable. Why is it that the session variable is always POSTED_VALUE? Is that an example?

Blank Avatar
owen
04 February 2009


hi ben,

do you think this plugin could be used to pass a logged in members ID and login status to another non-EE on-line app hosted on a sub-domain of the main EE domain?

Blank Avatar
Ben
07 February 2009


Jeff: in the example, the posted variable language is set as a session variable. just change language to the name of your posted variable.

owen: it may be possible if you can access the session variable from the non EE app, for example:

{exp:session_variables:set name="member_id" value="{member_id}"}

Blank Avatar
Kirk
10 February 2009


Should this work for accessing the session variable in a non-EE PHP app?

if(isset($_SESSION['language'])){
$language = $_SESSION['language'];
} else {
$language = 'no language';
}


» View more comments


Leave A Comment

name (required)
email (required but will be kept completely private)
website