How to Unserialize return False in PHP _php tips

Source: Internet
Author: User
Tags php programming serialization

The example of this article tells the PHP Unserialize return false solution, share for everyone reference. The specific methods are as follows:

PHP provides serialize (serialization) and Unserialize (deserialization) methods.
Using serialize serialization, you can then use unserialize deserialization to get the original data.

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

<?php 
$arr = Array ( 
  ' name ' => ' Fdipzone ', ' 
  gender ' => ' Male ' 
); 
 
$str = serialize ($arr); Serialization of 
Echo ' serialize str: '. $str. ' \r\n\r\n "; 
 
$content = Unserialize ($STR); Deserialize 
the echo "Unserialize str:\r\n"; 
Var_dump ($content); 
? > 

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 ']=> 
 string (4) "Male" 
} 

But the following example deserialization returns false

<?php 
$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: "Moving in Beijing, Beijing, China" s:4: "Miao"; s:1: "5";} '; 
Var_dump (Unserialize ($STR)); BOOL (false) 
?> 

After examining the serialized string, the problem is found in two places:

S:5: "url"
s:29: "Http://www.baidu.com/test.html"
Both of these should be
S:3: "url"
S:30: "Http://www.baidu.com/test.html"

This problem occurs because the encoding of the serialized data is inconsistent with the encoding at the time of deserialization, for example, the database is latin1 and UTF-8 characters are not the same length.
In addition, there may be a single double quote, ASCII character "" "is resolved to" "," in C is a string of Terminator equals Chr (0), error resolution after 2 characters.
\ r can also cause problems when calculating the length.

The workaround is as follows:

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); 
  Return Unserialize ($serial _str); 
 

Example:

Echo ' <meta http-equiv= ' Content-type "content=" text/html; Charset=utf-8 ">"; 
 
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:" Beijing, China Beijing mobile "; s:4:" Miao "; s:1:" 5 ";} '; 
 
Var_dump (Unserialize ($STR));  False 
 
Var_dump (Mb_unserialize ($STR));//correct 

The Mb_unserialize method of filtering \ R can be deserialized successfully using a single double quote that has been processed.

Use 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)" Beijing, Peking City, China Mobile " 
 [Miao]]=> 
 string (1)" 5 " 
}

I hope this article will help you with the learning of PHP programming.

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.