Session Variables Plugin
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.
Current Version: 1.0 (released 7 April 2008)
* Works with ExpressionEngine 1.6 and higher.
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.
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 variable
{exp:session_variables:set name="language" value="POSTED_VARIABLE"}
// 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_VARIABLE 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.
Installation
- Download and unzip session_variables.zip
- Upload pi.session_variables.php to the system/plugins folder.
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:







Comments
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 !
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
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).
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
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))
{
}
Mark
18 November 2008
I keep getting:
Session Plugin: name and value parameters must be entered
Help. What am I doing wrong?
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"}
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?
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?
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}"}
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';
}
Ben
10 February 2009
Kirk: yes, looks fine to me
Kirk
10 February 2009
I'm using your example to set a variable in EE, then trying to access it in CodeIgniter, and my code isn't working. Maybe it's an issue with CodeIgniter's sessions.
I did use this plugin to solve another EE issue, so thanks!
hcabbos
05 March 2009
Hi. I can't get this to work within a {exp:weblog:entries} tag. I can write the value of the variable outside of the tag but once I put it inline it doesn't output. Here's my code:
{exp:weblog:entries weblog="capability-category-overview" disable="member_data|pagination|trackbacks" sort="desc" category="{exp:session_variables:get name="prevCat"}&{category_id}"}
Ben
05 March 2009
hcabbos: yeah it doesn't surprise me that it doesn't work. the weblog module isn't parsing it. have you tried using the parse parameter?
{exp:weblog:entries weblog="capability-category-overview" disable="member_data|pagination|trackbacks" sort="desc" category="{exp:session_variables:get name="prevCat"}&{category_id}" parse="inward"}
not sure if that will work or if its even possible though. might be worth posting on the EE forums.
hcabbos
06 March 2009
Ben, thanks. That worked perfectly. I ran into another issue using your plugin which I'll probably post on the EE forums as you suggested. Here it is: Why can't I use your plugin to assign a value to an EE user-defined variable like so:
{assign_variable:my_testvariable="{exp:session_variables:get name="sessCapabilityCategoryID"}"}
I guess I'm finding hard to understand how your plugin could be used within EE tags if parse="inward" is not an option. I see your plugins use in writing to the document but that just seems so limited. Is it a restriction EE is placing?
hcabbos
06 March 2009
And in case you're wondering why I'd want to assign the session variable to an EE user-defined variable, well because I know for sure that an EE variable would work where your code wouldn't.
I appreciate your insight.
Ben
08 March 2009
yes i understand why you'd want to do that, however that is exactly why it is not possible - dynamically assigned global variables are parsed before plugins. this is one of EE's trickier and often frustrating characteristics but you just have to work around it. one of the most useful wiki pages in my opinion:
http://expressionengine.com/wiki/Parse_Order/
Jeff
19 March 2009
I'm setting a PHP session variable in a plugin (where I have session_start();). In the EE template that calls this plugin I can access the PHP session variable using this cool plugin! However, if a navigate to another template in EE (that does NOT contain "my" plugin that creates the PHP session variable), I can't access the PHP session variable with the same code that works on the first page?!
I'm thinking that if I set a PHP session variable in a plugin one one EE page/template, then I should be able to access that same session variable on other EE pages/templates??
Thanks,
JM
Sean Gates
26 March 2009
Your documentation with the plugin is wrong. I copy/pasted it and it was broken.
Bad code:
{exp:session:set name="my_name" value="my_val"}
Please update it so that others don't get bitten by this. Thanks for the plugin, though. ;)
Ben
27 March 2009
hi sean, where did you find this piece of code, i can't seem to find it in the documentation.
Don
26 May 2009
Works great but for one issue: if i reload the page and try to set new values in the session variables they don't take. i tried using the delete function first, but still nothing.
Leave A Comment