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.

1 Answer 1

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

2
  • Excellent solution! Your code snippet was working right away with Joomla! 3.4.3 Stable. Commented Aug 13, 2015 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. Commented Aug 13, 2015 at 15:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.