Cake comes preset to save session data in three ways: as temporary files inside of your Cake installation, using the default PHP mechanism, or serialized in a database. By default, Cake uses PHP’s default settings. To override this in order to use temp files or a database, edit your core configuration file at /app/config/core.php. Change the CAKE_SESSION_SAVE constant to ‘cake’, ‘php’, or ‘database’, depending on your application’s needs.
<?php define('CAKE_SESSION_SAVE', 'php');
In order to use the database session storage, you’ll need to create a table in your database. The schema for that table can be found in /app/config/sql/sessions.sql.
The Cake session component is used to interact with session information. It includes basic session reading and writing, but also contains features for using sessions for error and reciept messages (i.e. “Your data has been saved”). The Session Component is available in all Cake controllers by default.
Here are some of the functions you’ll use most:
Checks to see if the current key specified by $name has been set in the session.
Deletes the session variable specified by $name.
Returns the last error created by the CakeSession component. Mostly used for debugging.
string $key = ‘flash’
Returns the last message set in the session using setFlash(). If $key has been set, the message returned is the most recent stored under that key.
Returns the session variable specified by $name.
Renews the currently active session by creating a new session ID, deleting the old one, and passing the old session data to the new one.
string $layout = ‘default’
string $key = ‘flash’
Writes the message specified by $flashMessage into the session (to be later retrieved by flash()).
If $layout is set to ‘default’, the message is stored as
'<div class="message">'.$flashMessage.'</div>'. If $default
is set to an empty string (‘’) then the message is stored just as it has
been passed. If any other value is passed then the message is stored
inside the Cake view specified by $layout.
Params has been placed in this function for future usage. Check back for more info.
The $key variable allows you to store flash messages under keys. See flash() for retreiving a flash message based off of a key.
Returns true if the session is valid. Best used before read() operations to make sure that the session data you are trying to access is in fact valid.
Writes the variable specified by $name and $value into the active session.