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)))))));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算