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.
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:
- Open the session_variables_2_0 folder
- Upload the entire session_variables folder to the system/expressionengine/third_party/ folder
For EE1.6:
- Open the session_variables_1_6 folder
- 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)
- Initial release


6. 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.
Andy M
31 July 2009
Ben, the bad code Sean reported is in the Plugin Usage
// ----------------------------------------
// Plugin Usage
// ----------------------------------------
// This function describes how the plugin is used.
// Make sure and use output buffering
function usage()
{
ob_start();
?>
To set a session variable:
{exp:session:set name='my_name' value='my_value'}
Ben
31 July 2009
thanks andy, i'll fix it
Matteo
05 August 2009
Ben,
this is exactly the plugin I was looking for. Thanks!
M
cheap web hosting
26 August 2009
Thanks for the pluging, I was searching for it for a long time.
Steve
16 September 2009
Just wanted to point out that it appears the information in usage() should show "exp:session_variables" and not "exp:session".
Good plugin. Thanks.
Steve
16 September 2009
Apologies for the dupe. Looks like it was already reported.
Carl Crawley
29 October 2009
Hey! Great plugin - I'm trying to pass my session variable into another plugin and it doesn't seem to be working correctly. I'm pretty sure it's something to do with the parse order or something?
i.e. I have
{exp:session_variables:set name="img_width" value="{cell_5}"}
where {cell_5} is from my weblog entry. and then outside of my weblog loop, I have :
{exp:imgsizer:size src="{img_file}" auto="{exp:session_variables:get name="img_width"}"}
But it's not working - however, if I replace the exp:session_variables tag with a numeric value, it works.
Many thanks for your anticipated advise ;-)
Carl
Ben
01 November 2009
hi carl, yes its probably something to do with parse order, which can be quite tricky in EE. if you're setting and retrieving the session variable on the same page then why bother using it though? why not just wrap the imgsizer tag within a weblog:entries tag pair? i think that would solve it and be a better solution.
Jim
15 February 2010
Is there way way to use these with EE conditionals? I'd like to use a single view template for different status's of articles (news, workshops, demos) where only 1 status uses session objects. Right now, if the session object is empty (the data doesn't exist) the plugin gives an error.
It'd be great if the logic was:
{if "{status}" == "status_with_values"}
{exp:session_variables:set name="sessobj" value="{cms_value}"}
{/if}
This way if my field doesn't have data, the plug in doesn't crash the page.
Any way around this do you think??
Ben
16 February 2010
the thing is that the expression will be parsed before the conditional according to EE's template parsing order. what you could do though is prevent the error from being displayed by changing the return error line 39 to simply:
return;
Evan
05 March 2010
I'm trying to get the session to store the users screen resolution so that I can pull it up later in the page layout code. (Trying something with the imgsizer plugin to make fullscreen backgrounds). Here's what I have, and it's not working.
[code]
width = screen.width;
height = screen.height;
[removed]ln('{exp:session_variables:set name=\"screenW\" value=\"' + width + '\"}');
[removed]ln('{exp:session_variables:set name=\"screenH\" value=\"' + height + '\"}');
</script>
[/code]
My thought is that this would parse out the height and width, put them within the exp:session_variables:set tag, and away we go.
on a page that isn't processing the EE code, I get (on my screen at least)
[code]
{exp:session_variables:set name="screenW" value="1920"}
{exp:session_variables:set name="screenH" value="1080"}
[/code]
but once I stick that into the EE framework it tells me that
The following errors were encountered
Session Plugin: name and value parameters must be entered
Am I doing something retarded here? Is this sort of hijinks possible?
Evan
05 March 2010
There are some "
Ben
05 March 2010
quite often tags within a block of javascript code are not parsed.
also you're trying to put javascript code into an EE template tag. the problem is that the tag is parsed before the resulting code is sent to screen and hence before the javascript is executed.
what about setting the width and height of the image dynamically with javascript rather than using the imgsizer plugin? something like:
<img id="backImage" src="...">
and then with some javascript
backImage = document.getElementById('backImage');
backImage.style.width = "1270px";
Patrick M
09 March 2010
I dig the concept of the plugin but I just couldn't get it to play well.
1. I agree with Jeff concerning the session variable is always POSTED_VALUE when using it in this aspect. If a form posts a value for name ($_POST['name']) then {exp:session_variables:set name="name" value="POSTED_VARIABLE"} should pick up on that but it only stays as POSTED_VALUE. Does it make a difference if PHP is on or whether the parsing stage should be INPUT or OUTPUT.
2. I was excited to use this in the {exp:query} module but I could only get the sessionid when viewing the sql in debugging mode. SELECT COUNT(*) AS count FROM (SELECT exp_vehicles_import.make WHERE exp_vehicles_import.make = 'M20o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr' <--- This should've been the Session Variable. It does work when not in a {exp:query} tag but that's what I needed it for.
3. Good idea though! Keep it up! :)
Jim Pannell
09 March 2010
@Kirk - that's a great question. I'm also interested in finding that out.
Ben
09 March 2010
regarding the posted variable issue, i must admit that there was a mistake in the documentation. it should be set as follows:
{exp:session_variables:set name="name" value="POSTED_VALUE"}
please test it out and let me know if you have any problems with it. sorry about the blunder!
Jeff
11 March 2010
HI, I'm trying to use this instead of native PHP sessions but what I need to do is create arrays and put them into the session. Is that possible?
Ben
11 March 2010
no, only strings can be used. but you could try using php's serialize function to convert an array into a string first. or simply use something like this:
$string = implode('|', $array);
and then:
$array = explode('|', $string);
Steve
21 March 2010
Hi there, thanks for the plugin, but I've got an issue I can't resolve: I set a session variable on a landing page, to record an affiliate Id, before using the EE redirect (set to 'location') to send the user to the destination page, via the site/index page. Although the variable is set on the landing page, it seems to be 'unset' on the site/index page.
Do you know why that could be? I'm stumped.
Thanks
Steve
Jugs
17 April 2010
Thanks for this excellent plugn!
Just a quick question...Is there anyway I can access setter and getter methods from within the mod (php) file ? I want to set a few variables in the session after the user posts some data.
Thanks
wordpress programmer
21 June 2010
Hi..,
This is useful to me. This script is I am using to my client web site
creation.
Thanks.
intralimina
25 June 2010
Hi, this plugin works wonders for me when I use it with a regular form, but I haven't been able to get it to work with Solspace's FreeForm. When I try to add session variables using FreeForm I get back the literal "POSTED_VALUE" in my session variables instead of the value that was posted.
Do you know if there are any issues with Session Variables and FreeForm, or ways to work around them?
Let me know if you need more info. Thanks so much for your time.
Ben
27 June 2010
it could be that FreeForm is removing all post variables that it doesn't recognise before the Session Variables plugin is called...
Nate
20 July 2010
Hi Ben,
I'm thinking of using your plugin to store a usernames and passwords during a session on my EE 1.6 Site.
I'm running into a bit of a hiccup when trying to user the session variable I'm storing in a conditional if statement
for example:
{if PASSWORD(from session_variable) == (the correct value)}
Execute the page code
{if:else}
Redirect to the login page
{/if}
Is this possible? If so, what syntax would I use to access the session variable within the conditional statement there?
Ben
21 July 2010
you would use:
{if "{exp:session_variables:get name="language"}" == "xyz"}
» View more comments
Leave A Comment