views:

57

answers:

2

If I do a print_r($_SESSION) in my page.ctp I get:

Array
(
    [Config] => Array
        (
            [userAgent] => b3346028c15f82ac5d4b25c4f50d8718
            [time] => 1281034201
            [timeout] => 100
        )

    [manualLogout] => 1
    [Message] => Array
        (
        )

    [Auth] => Array
        (
            [redirect] => /events/add/controller:events
        )

    [facebookSynced] => 1
)

The var facebookSynced I set in my controller with $this->Session-write() - and there it is in the session as expected. But when I do a pr($this->Session) or a pr($session) from page.ctp I get:

SessionHelper Object
(
    [helpers] => Array
        (
        )

    [__active] => 1
    [valid] => 
    [error] => 
    [_userAgent] => b3346028c15f82ac5d4b25c4f50d8718
    [path] => /
    [lastError] => 
    [security] => medium
    [time] => 1281016202
    [sessionTime] => 1281034202
    [watchKeys] => Array
        (
        )

    [id] => 
    [host] => 
    [timeout] => 
    [base] => /Eclipse/Calc_1.3.2/trunk
    [webroot] => /Eclipse/Calc_1.3.2/trunk/
    [here] => /Eclipse/Calc_1.3.2/trunk/users/login
    [params] => Array
        (
            [controller] => users
            [action] => login
            [named] => Array
                (
                )

            [pass] => Array
                (
                )

            [plugin] => 
            [form] => Array
                (
                )

            [url] => Array
                (
                    [url] => users/login
                )

            [models] => Array
                (
                    [0] => User
                )

        )

    [action] => login
    [data] => 
    [theme] => 
    [plugin] => 
)

How do I access my session with the facebookSynced var in it, and what is the difference between these two 'sessions'. Extra info: in core.php I have:

Configure::write('Session.save', 'php');
Configure::write('Session.cookie', 'CAKEPHP');
Configure::write('Session.save', 'custom_sesh');
Configure::write('Session.timeout', '180');
Configure::write('Session.checkAgent', true);
Configure::write('Session.start', true);

The contents of custom_sesh is just one line:

ini_set('session.cookie_lifetime', 0);
+2  A: 

The first result from print_r($_SESSION) is a list of the values of the actual session variable.

The second result from pr($this->Session) shows the contents of CakePHP's Session helper object.

Both are entirely different things. If you wanted to access the value of your session variable facebookSynced you would probably want to do something like:

$foo = $_SESSION["facebookSynced"];
nukefusion
While this works, if you're trying to stay within the framework, I'd recommend DavidYell's solution. But have an upvote for being absolutely correct.
Travis Leleu
Yes I'm trying to stay in the framework - like I commended to David - I can't get the variable through $session or $this->Session - I think it should be there... do you have any ideas?
Mark Flint
+1  A: 

In your view you should be using the Session helper.

echo $session->read('Key.value');

Where you have written in something like, in your controller,

$this->Session->write('Key.value','example');
DavidYell
The var I want is not in $session (or in $this->Session), see the pr() dump of SessioHelper in my question, but it is in $_SESSION - this is why I am puzzled.
Mark Flint
My point is, you must write it into the session using the Cake helper in order for it be available to cake. If you want to stick with the framework, this is the method you will need. Also check in app/config that your sessions are being stored, and your security settings
DavidYell
I thought the session helper stores and reads into the php $_SESSION (presuming that's the method set up in core.php) Am I wrong? nukefusion (above) says "Both are entirely different things"...?
Mark Flint
Yes but Cake will place your variables into other places in the session, so whilst you might have called it 'foo' it may not be `$_SESSION['foo']` specifically
DavidYell