Understanding CakePHP Sessions
Written on 26 Oct 2006
CakePHP is a PHP framework which is turning out to be most useful, and very flexible. I've embarked on a few little projects with it, and thought I would spread a little understanding with regards to the session component - as I struggled to find a concise set of examples to help me on my way.
By default, a CakePHP application will automatically create a session instance when you're browsing through it - this is good, but it's not that obvious about where to go after this. If you were to put the following code into a controller:
The contents of the session variable would be printed out (print_r() dumps the contents of an array to screen). Without any influence over the session data, it would look like this:
(Bear in mind that the numbers & characters will all be different for you.)
If you are looking to disable this default session behaviour, set the
AUTO_SESSION constant to
false in /app/config/core.php.
Writing a variable into the session
Anyway, let's get onto some more interesting subject stuff - making the Session component work for us. If you want to write one value into the session, it's merely a case of doing:
If you look at a dump of the session array (
print_r($this -> Session -> read())) you will see that right at the end of the array, there's the text "[variable] => value" - magic.
Reading a variable from the session
If you want to use this session variable anywhere in your application, you can simply do:
This will return the string "value".
Putting objects into the session
Handily, you can store entire objects in the Session component - useful for passing information backwards and forwards regarding a user account/profile or the contents of a shopping cart. All you need to do is change the "value" part of the above example to be an array instead of a standard string value. For example:
In the above working example we expect a form to have been submitted to the controller containing a "User/email" value - we then look in the Users database table to find a row with the given e-mail address. After we've found it, we store it into the Session component. Please note the
$user['User'] is not the entire array as we only want the user information - there may well be other rows returned by CakePHP depending on your model associations (e.g. A Profile could belong to a User, and our database query above would return the Profile data too.)
Considering the above example, how would we get specific data out of the Session component? Easy, but not obvious:
Closing/destroying a session
Most uses of destroying a session will for logging users out of a system - and it's made very easy. For neatness it's better to check and make sure the session is still valid before attempting to destroy it:
It's also courteous to redirect the user somewhere useful.
There's a little more to session handling within CakePHP - it's worth looking into it. Features such as checking values stored in a session, storing flash() messages in a session, deleting specific values etc etc. Have a gander at the CakePHP API documentation on the Session component to get up to speed. If you're not so sharp on CakePHP yet, check back to the examples above - they should outline the steps needed to use the other functions.
If anything has been unclear, post a comment and I'll get back to you - I went through the whole thing with a shade of briskness.