We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to unserialize a string from session

I use Libmemcached class to store my session, It work well !

I want to read other session, I can read from a session as string , but I can not unserialize it. How can I do?

// read session info
$session_data = $this->session->read($session_id);

// Readed string like this
// auth|a:5:{s:4:"role";s:5:"Media";s:2:"id";i:2652;s:10:"permission";s:6:"Manage";s:9:"skip_term";b:0;s:8:"is_admin";b:0;}Media|a:1:{s:4:"name";s:12:"メディア";}Clear\App\Dashboard\Plugin\SecurityPlugin|a:1:...............................

// unserialize
$session_data_array = unserialize($session_data); // return false

the problem is the string to unserialize, unserialize() can process this string a:5:{s:4:"role";s:5:"Media";s:2:"id";i:2652;s:10:"permission";s:6:"Manage";s:9:"skip_term";b:0;s:8:"is_admin";b:0;} you have to divide the chain in the following way (you have to improve it)

$string = 'auth|a:5:{s:4:"role";s:5:"Media";s:2:"id";i:2652;s:10:"permission";s:6:"Manage";s:9:"skip_term";b:0;s:8:"is_admin";b:0;}Media|a:1:{s:4:"name";s:12:"メディア";}Clear\App\Dashboard\Plugin\SecurityPlugin|a:1:...............................';

foreach(explode('}', $string) as $part) {
    list($key, $value) = explode('|', $part);

    $restoredArray = unserialize($part); // here you have your session data
}

Good luck

Great!, thank you for your code.

the problem is the string to unserialize, unserialize() can process this string a:5:{s:4:"role";s:5:"Media";s:2:"id";i:2652;s:10:"permission";s:6:"Manage";s:9:"skip_term";b:0;s:8:"is_admin";b:0;} you have to divide the chain in the following way (you have to improve it)

$string = 'auth|a:5:{s:4:"role";s:5:"Media";s:2:"id";i:2652;s:10:"permission";s:6:"Manage";s:9:"skip_term";b:0;s:8:"is_admin";b:0;}Media|a:1:{s:4:"name";s:12:"メディア";}Clear\App\Dashboard\Plugin\SecurityPlugin|a:1:...............................';

foreach(explode('}', $string) as $part) {
  list($key, $value) = explode('|', $part);

  $restoredArray = unserialize($part); // here you have your session data
}

Good luck