dream5694 2014-10-03 04:54
浏览 59
已采纳

PHP会话处理程序将会话存储为JSON而不是正确解码php序列化

I've implemented a session handler using DynamoDB. It is working correctly, except for when the response is returned from the server and I call json_decode on it, the nested php serialized session string is broken. I assume storing a serialized session inside a JSON object is not compatible? Is this an escaping issue?

Questions:

  1. Is there a way to get around this?
  2. Is it possible to serialize the session in JSON rather than php serialize() which I assume is used? This would probably solve the issue.

This is how the original looks when it comes from the DB (note: it's really long so I've cut the end off myself)

_sf2_attributes|a:4:{s:34:"_security.secured_area.target_path";s:36:"http://my.domain";s:10:"some_id";s:13:"abc123";s:8:"userData";O:27:"Some\Symfony\Bundle":49:{s:8:"�*�email";s:27:"

This is how it looks after calling json_decode on the JSON object this string is inside:

_sf2_attributes|a:4:{s:34:"_security.secured_area.target_path";s:36:"http://my.domain";s:10:"some_id";s:13:"abc123";s:8:"userData";O:27:"Some\Symfony\Bundle":49:{s:8:"

It appears to be truncated where the asterisk is in the original.

  • 写回答

1条回答 默认 最新

  • dsgft1486 2014-10-03 05:05
    关注

    json and serialization works fine in PHP

    class ClassA
        {
        public $publicMember;
        private $privateMember;
        protected $protected;
        }
    $object = new ClassA();
    var_dump(unserialize(json_decode(json_encode(serialize($object)))));
    

    But this issue could come from DynamoDB: Bug in DynamoDB / DynamoDB Session Handler for PHP (handling of NULL BYTES). It seems that it closed, but... you could add additional wrap with base64_encode before json_encode

    var_dump(unserialize(base64_decode(json_decode(json_encode(base64_encode(serialize($object)))))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用