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.

General Questions /

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

Sessionvalues on _config.php not available in SS3.x


Reply


6 Posts   485 Views

Avatar
Andre

Community Member, 126 Posts

8 May 2013 at 7:14am

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?

Avatar
Bambii7

Community Member, 254 Posts

9 May 2013 at 3:58pm

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') );

Avatar
Andre

Community Member, 126 Posts

9 May 2013 at 5:59pm

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.

Avatar
Andre

Community Member, 126 Posts

10 May 2013 at 7:40am

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.

Avatar
Bambii7

Community Member, 254 Posts

10 May 2013 at 2:38pm

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.

Avatar
Bambii7

Community Member, 254 Posts

10 May 2013 at 5:24pm

Edited: 10/05/2013 5:49pm

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.