Jump to:

23378 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Sessionvalues on _config.php not available in SS3.x

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: 399 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    Sessionvalues on _config.php not available in SS3.x Link to this post

    Hi there, has the Session Mechanism changed in SS3.x?

    On SS2.4 as I remember, Sessions (like Session::get('something')) where allready available in _config.php Files. In SS3.x this seems to have changed. Am I right? Is there any way to get Sessiion Variables back on _config files?

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Sessionvalues on _config.php not available in SS3.x Link to this post

    Don't really know if the internal mechanics of Session have changed.
    I can see the class in framework/control/Session.php
    A quick test on a local SS3 install seemed to work in the mysite/_config.php file

    Session::set('test','val');
    die( Session::get('test') );

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: Sessionvalues on _config.php not available in SS3.x Link to this post

    You are right, that Example works.

    But if you write a Session Variable somewhere else and try to read it from the _config File on the next request, it does'nt work.

    I have changed the Project Folder to another one by setting these Variables inside mysite/_config.php

    global $project;
    $project = 'cp_config';

    Maybe that's the reason, why it doesn't work for me.

  • Andre
    Avatar
    Community Member
    126 Posts

    Re: Sessionvalues on _config.php not available in SS3.x Link to this post

    Hi there, try the following code and you will see, what I mean:

    echo Session::get('test');
    Session::set('test','val');
    echo Session::get('test');

    theoretically, after the second Page reload, the first and the second echo should output "val", but it doesn't.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Sessionvalues on _config.php not available in SS3.x Link to this post

    Will have a test after lunch. I wonder if the config file is read before the session object is found... I never know at which point the config file is read.

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Sessionvalues on _config.php not available in SS3.x Link to this post

    Right... looks like Session::start isn't called before the loading of the _config file.

    Short story this will work, echoing val twice

    Session::start();
    echo Session::get('test');
    Session::set('test','val');
    echo Session::get('test');
    die();

    Long story
    index.php is hit with a request which loads framework/main.php first thing that does is load core/Core.php which loads config and sets up DB connections. After that at the end of main.php Director::direct is called to load the relevant pages and starts the session, after config is loaded. So session will be initialised and usable in side PageController but not by default in _config.php.

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