dozxos6346 2016-05-26 09:22
浏览 67
已采纳

Php Object to JSON返回奇怪的Json

I am working on a system where I retrieve user preferences and insert them into cookies with JSON because I have a Javascript file that needs to use the value in the cookie too.

Now, when i use JSON.Stringify() with javascript i get the data just as i expect it. enter image description here

However, when i go to check the cookie after it has run the PHP script i get this: enter image description here

Here is my code from the php file:

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event){
    if($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')){
        $selectedCategories = $event->getAuthenticationToken()->getUser()->getProfile()->getCategories();
        setcookie("userInterests",json_encode(get_object_vars($selectedCategories)),(time()+3*60*60*24*30));
    }
}

any help would be greatly appreciated!

  • 写回答

1条回答 默认 最新

  • douwanc63652 2016-05-31 08:30
    关注

    In my case I had multiple problems: firstly I tried to enter an object(that I thought was an array) into the cookie. This had the weird cookie as a result, because the PHP cookie only held the start string for the urlencode.

    Secondly, I didn't know that cookies automatically get urlencoded in PHP so that threw me of. If I just use urldecode now, at the point where I am returning the data from the cookie. It should just pass the javascript cookie without editing it and my php cookie will be useable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图