Workaround for Unserialize return False in PHP, unserializefalse_php tutorial

Source: Internet
Author: User
Tags learn php learn php programming php programming

Workaround for Unserialize return False in PHP, Unserializefalse


In this article, we describe the solution of Unserialize return False in PHP, and share it for your reference. Here's how:

PHP provides serialize (serialization) and Unserialize (deserialization) methods.
Once serialized using serialize, the original data can be obtained using unserialize deserialization.

Let's take a look at the following program examples:

<?php $arr = Array (   ' name ' = ' fdipzone ',   ' gender ' = ' male ');  $str = serialize ($arr); Serialize Echo ' serialize str: '. $str. " \r\n\r\n ";  

Output:

Serialize str:a:2:{s:4: "Name"; S:8: "Fdipzone"; s:6: "Gender"; s:4: "Male";}  Unserialize Str:array (2) {  ["name"]=>  string (8) "Fdipzone"  ["Gender"]=>  

But the following example deserializes returns false

Check the serialized string and discover 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:

UTF8 function mb_unserialize ($serial _str) {   $serial _str= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ ' $2\ "; '", $serial _str);   $serial _str= str_replace ("\ R", "", $serial _str);   Return Unserialize ($serial _str); }  //ASCII function asc_unserialize ($serial _str) {   $serial _str = preg_replace ('!s: (\d+): "(. *?)";! Se ', ' "s:". strlen ("$"). ": \" $2\ ";" ', $serial _str);   $serial _str= str_replace ("\ R", "", $serial _str);   

Example:

Echo '
 
   ;  UTF8 function mb_unserialize ($serial _str) {   $serial _str= preg_replace ('!s: (\d+): "(. *?)";! Se ', ' s: '. strlen (' $ "). ': \ ' $2\ "; '", $serial _str);   $serial _str= str_replace ("\ R", "", $serial _str);   Return Unserialize ($serial _str); }  $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:29: "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";} ';  Var_dump (Unserialize ($STR));  False  

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

Using unserialize:

BOOL (FALSE)

Using Mb_unserialize

Array (9) {  ["Time"]=>  int (1405306402)  ["Name"]=>  string (6) "New Morning"  ["url"]=>  String (1) "-"  ["word"]=>  string (1) "-"  ["RPage"]=>  string "http://www.baidu.com/ Test.html "  [" Cpage "]=>  string (1)"-"  [" IP "]=>  string" 117.151.180.150 "  [" Ip_ City "]=>  string (31)" Mobile "  [" Miao "]=>  string (1)" 5 "} in Beijing, Beijing, China

I hope this article will be helpful for you to learn PHP programming.


PHP unserialize () function, return false how to solve,

Return False indicates a problem with your data, check if your stored data is in the correct format.

Unserialize ($STR) in PHP returns FALSE, prints out $str and writes parentheses to succeed, asking why

This is a coding problem, the database encoding and PHP file encoding inconsistent.
You print out the text, after the copy paste, is actually a code conversion.

http://www.bkjia.com/PHPjc/882910.html www.bkjia.com true http://www.bkjia.com/PHPjc/882910.html techarticle PHP Unserialize return False resolution, Unserializefalse This example tells the PHP Unserialize return false resolution, share to everyone for your reference. The specific method is as follows: ...

  • 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.