2

Is there a way to read respectively parse Joomla session data in PHP as session_decode() and unserialize() are not working on that values?

In my component I need to read data from all active non-guest sessions (data column in the #__session database table) and check for the existence of certain variables.

2

In that field data is split into namespaces. Namespace and its data is separated by |, so if you use explode('|',$data) you will achieve array where odd index element is an data array and each even index element is an namespace. Here is an example how to decode this variable into namespaces:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('data')->from('#__session');
$db->setQuery($query);
$raw_data = str_replace('\0\0\0', chr(0) . '*' . chr(0), (string)$db->loadObject()->data);
$raw_data = explode('|',$raw_data);

$data = array();
for( $idx = 1, $ic=count($raw_data); $idx<$ic; $idx+=2 ) {
    $data[$raw_data[$idx-1]] = unserialize($raw_data[$idx]);
}

The $data variable will contain session data split into separate array elements where array key is namespace. The default namespace should be first and its index should be __default

  • Excellent solution! Your code snippet was working right away with Joomla! 3.4.3 Stable. – Mario Neubauer Aug 13 '15 at 9:30
  • Should, I tested it on Joomla 3.4.3, of course this is for single session element. If you want to check all sessions you will need to change it a little bit. Probably create a function from this and use it on every session element. Also remember that if you have many sessions trying to parse all will have huge performance downfall. – Artur Stępień Aug 13 '15 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.