PHP function Unserialize Data return false problem analysis

Source: Internet
Author: User

Unserialize This issue was found by a emlog forum user in the use of the Times wrong

The performance of the problem is as follows:

The Emlog cache is saved by serializing the data Object (array) of PHP (serialize) and storing it as a file, and then deserializing (unserialize) the cache string to read the data when the cache is read.

The cached serialized data that I fetch from the user is:

A:1:{s:8: "Kl_album"; A:4:{s:5: "title"; S:12: "Wonderful Moment"; s:3: "url"; s:41: "Http://www.kaisay.cn/?plugin=kl_album"; S:8: " Is_blank "; s:7:" _parent "; s:4:" Hide "; s:1:" N ";}}

I look at the people who understand serialization will say, this data is very normal Ah, no problem. But the string is unserialize directly, the value returned is a false;

Code

Var_dump (Unserialize (' A:1:{s:8: "Kl_album"; A:4:{s:5: "title"; S:12: "Wonderful Moment"; s:3: "url"; s:41: "http://www.kaisay.cn/? Plugin=kl_album "; s:8:" Is_blank "; s:7:" _parent "; s:4:" Hide "; s:1:" N ";}} ');

Run results

What is the problem? The answer is s:41: "Http://www.kaisay.cn/?plugin=kl_album"

The serialized string http://www.kaisay.cn/?plugin=kl_album the length of the string is 41, but we have only 37 characters to count. This is the problem that causes PHP to deserialize the string invalidation.

If you change the length of the string to 37, the program will be successfully deserialized

Code:

Var_dump (Unserialize (' A:1:{s:8: "Kl_album"; A:4:{s:5: "title"; S:12: "Wonderful Moment"; s:3: "url"; s:37: "http://www.kaisay.cn/? Plugin=kl_album "; s:8:" Is_blank "; s:7:" _parent "; s:4:" Hide "; s:1:" N ";}} ');

After Google only found that the problem abroad has been a lot of netizens encountered, in the official manual unserialize function page comments There are many netizens in the discussion and study the solution of the problem.

There are many possible reasons for this, and the biggest possibility is that the encoding and deserialization at the time of serializing the data is not the same as causing the length of the string to deviate. For example, database encoding Latin1 and UTF-8 character lengths are not the same.

Summary: Solution:

Situation One: UTF-8

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);}

Scenario Two: ASC

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);}

PHP function Unserialize Data return false problem analysis

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.