Solution for returning false to unserialize in php _ PHP Tutorial

Source: Internet
Author: User
Php unserialize returns false. Php unserialize returns false solution php provides serialize (serialization) and unserialize (deserialization) methods. After serialize is used for serialization, you can use unserialize for deserialization to return false for unserialize in php.

Php provides the serialize (serialization) and unserialize (deserialization) methods.

After serialize is used for serialization, you can use unserialize for deserialization to obtain the original data.

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

1

2

3

4

5

6

7

8

9

10

11

12

13

$ Arr = array (

'Name' => 'fdipzone ',

'Gender' => 'male'

);

$ Str = serialize ($ arr); // serialization

Echo 'serialize str: '. $ str. "\ r \ n ";

$ Content = unserialize ($ str); // deserialization

Echo "unserialize str: \ r \ n ";

Var_dump ($ content );

?>

Output:

1

2

3

4

5

6

7

8

9

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"

}

However, in the following example, deserialization will return false.

1

2

3

4

$ 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 Mobile"; s: 4: "miao"; s: 1: "5 ";}';

Var_dump (unserialize ($ str); // bool (false)

?>

Check the serialized string and find the problem in two places:

S: 5: "url"
S: 29: "http://www.baidu.com/test.html"
The two parts should be
S: 3: "url"
S: 30: "http://www.baidu.com/test.html"

This problem occurs because the encoding during data serialization is inconsistent with the encoding during deserialization, for example, the database is latin1 and the UTF-8 character length is different.
In addition, there may be a single double quotation mark. the ascii character "\ 0" is parsed as '\ 0', and \ 0 in C is the string terminator equal to chr (0 ), 2 characters after error parsing.
\ R also causes problems when calculating the length.

The solution is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

// Utf8

Function mb_unserialize ($ serial_str ){

$ Serial_str = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $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"). ": \" $2 \ ";" ', $ serial_str );

$ Serial_str = str_replace ("\ r", "", $ serial_str );

Return unserialize ($ serial_str );

}

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Echo' ';

// Utf8

Function mb_unserialize ($ serial_str ){

$ Serial_str = preg_replace ('! S :( \ d + ):"(.*?) ";! Se ', "s:'. strlen ('$ 2').': \" $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 Mobile"; s: 4: "miao"; s: 1: "5 ";}';

Var_dump (unserialize ($ str); // false

Var_dump (mb_unserialize ($ str); // correct

The mb_unserialize method for filtering \ r can be deserialized successfully by processing single double quotes.

Use unserialize:

Bool (false)

Use mb_unserialize

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Array (9 ){

["Time"] =>

Int (1405306402)

["Name"] =>

String (6) "Xinchen"

["Url"] =>

String (1 )"-"

["Word"] =>

String (1 )"-"

["Rpage"] =>

String (30) "http://www.baidu.com/test.html"

["Cpage"] =>

String (1 )"-"

["Ip"] =>

String (15) "117.151.180.150"

["Ip_city"] =>

String (31) "Beijing, China Mobile"

["Miao"] =>

String (1) "5"

}

Php provides the serialize (serialization) and unserialize (deserialization) methods. After serialize is used for serialization, you can use unserialize for deserialization...

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.