Workaround for PHP unserialize return False

Source: Internet
Author: User
Tags vars

PHP provides serialize (serialization) and Unserialize (deserialization) methods.

Once serialized using serialize, the original data can be obtained using unserialize deserialization. (Change the table structure or do the following)

[PHP]View Plaincopy
  1. <?php
  2. $arr = Array (
  3. ' name ' = ' Fdipzone ',
  4. ' gender ' = ' male '
  5. );
  6. $str = Serialize ($arr); //Serialization
  7. echo ' serialize str: '.   $str."\r\n\r\n";
  8. $content = unserialize ($str); //deserialization
  9. echo "Unserialize str:\r\n";
  10. Var_dump ($content);
  11. ?>

Output:

[Plain]View Plaincopy
    1. Serialize str:a:2:{s:4: "Name"; S:8: "Fdipzone"; s:6: "Gender"; s:4: "Male";}
    2. Unserialize STR:
    3. Array (2) {
    4. ["Name"]=>
    5. String (8) "Fdipzone"
    6. ["Gender"]=>
    7. String (4) "Male"
    8. }


But the following example deserializes returns false

[PHP]View Plaincopy
    1. <?php
    2. $str = ' a:9:{s:4: ' time '; i:1405306402;s:4: "Name"; S:6: "New Morning"; s:5: "url"; s:1: "-"; s:4: "word"; s:1: "-"; S:5: "RPage"; s : "Http://www.baidu.com/test.html"; s:5: "Cpage"; s:1: "-"; s:2: "IP"; s:15: "117.151.180.150"; s:7: "Ip_city"; s:31: "  S:4, Beijing, China: "Miao"; s:1: "5";} ';
    3. Var_dump (Unserialize ($str)); //bool (FALSE)
    4. ?>


Check the serialized string and find out that the problem is in two places

S:5: "url"

s:29: "Http://www.baidu.com/test.html"

These two places should be

S:3: "url"

S:30: "Http://www.baidu.com/test.html"


This problem occurs because the encoding when serializing the data is inconsistent with the encoding when deserializing, for example, the database is latin1 and the UTF-8 character length is not the same.

There is also the possibility of a single double quotation mark, the ASCII character "\" is resolved to ' Terminator ', in C is the string is equal to Chr (0), error parsing after 2 characters.

\ r also causes problems when calculating the length.


Here's how to fix it:

[PHP]View Plaincopy
  1. Utf8
  2. function mb_unserialize ($serial _str) {
  3. $serial _str= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ '  $2\ "; '", $serial _str);
  4. $serial _str= str_replace ("\ r", "", $serial _str);
  5. return unserialize ($serial _str);
  6. }
  7. Ascii
  8. function asc_unserialize ($serial _str) {
  9. $serial _str = preg_replace ('!s: (\d+): "(. *?)";! Se ', ' "s:". strlen ("$"). ": \"  $2\ ";" ', $serial _str);
  10. $serial _str= str_replace ("\ r", "", $serial _str);
  11. return unserialize ($serial _str);
  12. }


Example:

[PHP]View Plaincopy
  1. echo ' <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8" > ";
  2. Utf8
  3. function mb_unserialize ($serial _str) {
  4. $serial _str= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ '  $2\ "; '", $serial _str);
  5. $serial _str= str_replace ("\ r", "", $serial _str);
  6. return unserialize ($serial _str);
  7. }
  8. $str = ' a:9:{s:4: ' time '; i:1405306402;s:4: "Name"; S:6: "New Morning"; s:5: "url"; s:1: "-"; s:4: "word"; s:1: "-"; S:5: "RPage"; s : "Http://www.baidu.com/test.html"; s:5: "Cpage"; s:1: "-"; s:2: "IP"; s:15: "117.151.180.150"; s:7: "Ip_city"; s:31: "  S:4, Beijing, China: "Miao"; s:1: "5";} ';
  9. Var_dump (Unserialize ($str)); //False
  10. Var_dump (Mb_unserialize ($str)); //correct


The Mb_unserialize method of filtering \ r can be successfully deserialized using a single double-quote process.

[Plain]View Plaincopy
    1. Using Unserialize
    2. BOOL (FALSE)
    3. Using Mb_unserialize
    4. Array (9) {
    5. ["Time"]=>
    6. Int (1405306402)
    7. ["Name"]=>
    8. String (6) "New Morning"
    9. ["url"]=>
    10. String (1) "-"
    11. ["word"]=>
    12. String (1) "-"
    13. ["RPage"]=>
    14. String ("http://www.baidu.com/test.html")
    15. ["Cpage"]=>
    16. String (1) "-"
    17. ["IP"]=>
    18. String (15) "117.151.180.150"
    19. ["Ip_city"]=>
    20. String (31) "Mobile", Beijing, Beijing, China
    21. ["Miao"]=>
    22. String (1) "5"
    23. }

PHP unserialize Returns a workaround for false

Related Article

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.