How to handle cookie arrays in thinkphp3

Source: Internet
Author: User
Recently, when I was playing the thinkphp framework, it was really dizzy when I encountered a cookie. I found thinkphp2.0 information on the Internet, saying Think \ Lib \ Think \ Util \ Cookie. class. but actually thinkphp3.0 has moved the cookie to ThinkPHP \ Common \ functions. php.

Recently, when I was playing the thinkphp framework, it was really dizzy when I encountered a cookie. I found thinkphp2.0 information on the Internet, saying Think \ Lib \ Think \ Util \ Cookie. class. but actually thinkphp3.0 has moved the cookie to ThinkPHP \ Common \ functions. in php, use test. php tested the cookie function and found that the cookie will automatically store the array with json_encode connection. as mentioned in the manual, array storage is supported, but it has actually become a josn string, in addition, the original Chinese characters have all become characters such as "\ UXXXX", which is very strange. later I checked the original code of the cookie function and found that the characters were converted to json_encode and re-encoded, the decoding method is found, but the problem persists. you can use json_decode ($ _ COOKIE ["userinfo"]) to obtain the original array on the current page, however, after changing the page, the result is "NULL", and the value is read with $ str = cookie ("userinfo"), but the result is:

  1. Object (stdClass) #4 (3 ){
  2. ["Name"] => string (6) "zhang san"
  3. ["Code"] => int (123)
  4. ["Password"] => string (8) "22334455"
  5. }

$ Str is a class that is processed as a class.

But I want to get an array, so dump ($ _ COOKIE ["userinfo"]), the result is "{\" name \": \ "\ u5f20 \ u4e09 \", \ "code \": 123, \ "password \": \ "22334455 \"}", that is to say, all escape characters are added, except the numbers. after finding the cause, it is easy to solve the problem. use stripslashes ($ _ COOKIE ["userinfo"]); // after the escape characters are removed, perform the reverse encoding operation json_decode ($ str, true). finally, dump () prints the result and returns the correct array result.

  1. Array (3 ){
  2. ["Name"] => string (6) "zhang san"
  3. ["Code"] => int (123)
  4. ["Password"] => string (8) "22334455"
  5. }

The instance code is as follows:

  1. Require_once "ThinkPHP/Common/functions. php ";
  2. $ User = array (
  3. "Name" => "James ",
  4. "Code" => 123,
  5. "Password" => "22334455 ");
  6. Cookie (userinfo, $ user, time () + 3600 );
  7. Var_dump ($ _ COOKIE ["userinfo"]);
  8. // Re-read as an array
  9. $ Str = stripslashes ($ _ COOKIE ["userinfo"]); // remove escape characters
  10. $ Userinfo = json_decode ($ str, true );
  11. Var_dump ($ userinfo );
  12. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.