Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Session Class


Go to End


2 Posts   1475 Views

Avatar
Mo

Community Member, 541 Posts

31 October 2009 at 3:19am

Hi All,

Does anyone have any good docs/tutorials on working with the session class? There is not a lot I can find, and the API reference says it may be being depreciated.

Is this the case? Because the forms still seem to run off them (for error messages and what not)?

Cheers,

Mo

Avatar
dalesaurus

Community Member, 283 Posts

31 October 2009 at 4:11am

Edited: 31/10/2009 4:12am

I have worked extensively with them and this is an area where the SS team has done an excellent job at abstracting them. So much in fact there really isn't much to document.

$x = 'remember this';
$y = array('some' => 'stuff', 'nest' => array('of'=>'other'));
$z = DataObject::get_by_id('Page',43);

// Save stuff in the session
Session::set('Identifier.X',$x);
Session::set('MyLongIdString', serialize($y));
Session::set('ThatPage', serialize($z));

// Fetch them back, some other page load, some other day
$newX = Session::get('Identifier.X');
$newY = unserialize(Session::get('MyLongIdString'));
$newZ = unserialize(Session::get('ThatPage'));

// Don't really put objects like $z in session, 
// it makes them way heavy and they'll chew up 
// a ton of resources as they have to be loaded from
// disk every time a page is served (depending on
// your PHP session storage settings)
Session::clear('ThatPage');

Thats all there really is to it. Create a unique identifier string and set/get. You will have that data persistently available until the session expires. Do make a habit of keeping it cleaned up because it can get expensive to load/save huge sessions.

Also remember to serialize/unserialize any complex objects (like anything not a string).