我将会话数据存储在redis中 - 我需要从另一个甚至不用PHP编写的应用程序中获取它,但是当我尝试在我之前从redis获取的存储会话字符串中使用默认的php unserialize方法时 - 我得到了false
示例序列化会话字符串:
_sf2_attributes|a:2:{s:26:"_security.main.target_path";s:21:"http://taxi/dashboard";s:14:"_security_main";s:787:"C:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":699:{a:3:{i:0;s:60:"$2y$13$0LWym6x/aYM.TJkGlKreCO7Pc.lw5sovpbOYdJ3LMEqURiXjsKGVq";i:1;s:6:"public";i:2;s:591:"a:4:{i:0;C:22:"TaxiBundle\Entity\User":253:{a:8:{i:0;s:60:"$2y$13$0LWym6x/aYM.TJkGlKreCO7Pc.lw5sovpbOYdJ3LMEqURiXjsKGVq";i:1;s:31:"s28cx1llhk0gk8cskggk404ko0kw808";i:2;s:10:"mrandersen";i:3;s:10:"mrAndersen";i:4;b:1;i:5;i:3;i:6;s:24:"mrandersen2004@yandex.ru";i:7;s:24:"mrandersen2004@yandex.ru";}}i:1;b:1;i:2;a:2:{i:0;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"\x00Symfony\Component\Security\Core\Role\Role\x00role";s:16:"ROLE_SUPER_ADMIN";}i:1;O:41:"Symfony\Component\Security\Core\Role\Role":1:{s:47:"\x00Symfony\Component\Security\Core\Role\Role\x00role";s:9:"ROLE_USER";}}i:3;a:0:{}}";}}";}_sf2_flashes|a:0:{}_sf2_meta|a:3:{s:1:"u";i:1485465482;s:1:"c";i:1485459877;s:1:"l";s:6:"864000";}
哪里可以找到symfony默认序列化算法? THX
答案 0 :(得分:2)
Symfony没有任何构建会话序列化算法,它使用内置的PHP与常规serialize
/ unserialize
不同。检查php.net上的this评论,您可以在其中找到手动反序列化php会话数据的方法。基本上它取决于session.serialize_handler设置,但您正在寻找的算法是这样的(只是从我提供的第一个链接复制):
<?php
class Session {
public static function unserialize($session_data) {
$method = ini_get("session.serialize_handler");
switch ($method) {
case "php":
return self::unserialize_php($session_data);
break;
case "php_binary":
return self::unserialize_phpbinary($session_data);
break;
default:
throw new Exception("Unsupported session.serialize_handler: " . $method . ". Supported: php, php_binary");
}
}
private static function unserialize_php($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
if (!strstr(substr($session_data, $offset), "|")) {
throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
}
$pos = strpos($session_data, "|", $offset);
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
private static function unserialize_phpbinary($session_data) {
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
$num = ord($session_data[$offset]);
$offset += 1;
$varname = substr($session_data, $offset, $num);
$offset += $num;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
}