Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Session Class

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

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

Page: 1
Go to End
Author Topic: 985 Views
  • Mo
    Avatar
    Community Member
    508 Posts

    Session Class Link to this post

    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

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: Session Class Link to this post

    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).

    985 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.