I have a requirement for a current project to be able access the CakePHP session data for a script which is running outside of the framework. In normal circumstances this would be as simple as:
session_name('CAKEPHP'); session_start() print_r($_SESSION);
However in my case the session data is stored in the database. In this situation you need to do a slight bit more to retrieve the session data. Thought I would share the code here for anyone who may need it.
The data is stored in the database in a serialised form and the session_key is essentially stored in a cookie. Therefore the equivalent of the above is:
include_once('../config/database.php'); $db = new DATABASE_CONFIG(); $dbh = mysql_connect($db->default['host'],$db->default['login'],$db->default['password']); $dbn = mysql_select_db($db->default['database'], $dbh); $session_qry = mysql_query('SELECT `data` FROM `sessions` WHERE `id`="'.$_COOKIE['CAKEPHP'].'"', $dbh); $session_data = mysql_result($session_qry, 0, 'data'); session_start(); session_decode($session_data); print_r($_SESSION);
Hope this comes in useful.