php中unserialize返回false的解决方法
2015-01-24信息快讯网
这篇文章主要介绍了php中unserialize返回false的解决方法,是PHP程序设计中非常经典的问题,需要的朋友可以参考下
本文实例讲述了php中unserialize返回false的解决方法,分享给大家供大家参考。具体方法如下:
php 提供serialize(序列化) 与unserialize(反序列化)方法。
使用serialize序列化后,再使用unserialize反序列化就可以获取原来的数据。
先来看看如下程序实例:
<?php $arr = array( 'name' => 'fdipzone', 'gender' => 'male' ); $str = serialize($arr); //序列化 echo 'serialize str:'.$str."\r\n\r\n"; $content = unserialize($str); // 反序列化 echo "unserialize str:\r\n"; var_dump($content); ?>
输出:
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"
}
但下面这个例子反序列化会返回false
<?php
$str = 'a:9:{s:4:"time";i:1405306402;s:4:"name";s:6:"新晨";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:"miao";s:1:"5";}';
var_dump(unserialize($str)); // bool(false)
?>
检查序列化后的字符串,发现出问题是在两处地方:
s:5:"url"
s:29:"http://www.baidu.com/test.html"
这两处应为
s:3:"url"
s:30:"http://www.baidu.com/test.html"
出现这种问题的原因是序列化数据时的编码与反序列化时的编码不一致导致,例如数据库是latin1和UTF-8字符长度不一样。
另外有可能出问题的还有单双引号,ascii字符"\0"被解析为 '\0',\0在C中是字符串的结束符等于chr(0),错误解析后算了2个字符。
\r在计算长度时也会出问题。
解决方法如下:
// 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);
}
例子:
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').':\"$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:"新晨";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:"miao";s:1:"5";}';
var_dump(unserialize($str)); // false
var_dump(mb_unserialize($str)); // 正确
使用处理过单双引号,过滤\r的mb_unserialize方法就能成功反序列化了。
使用unserialize:
bool(false)
使用mb_unserialize
array(9) {
["time"]=>
int(1405306402)
["name"]=>
string(6) "新晨"
["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) "中国北京市 北京市移动"
["miao"]=>
string(1) "5"
}
希望本文所述对大家PHP程序设计的学习有所帮助。
ucenter通信原理分析
PHP中new static() 和 new self() 的区别介绍
php计划任务之ignore_user_abort函数实现方法
CI框架中site_url()和base_url()的区别
WampServer下安装多个版本的PHP、mysql、apache图文教程
PHP和Shell实现检查SAMBA与NFS Server是否存在
phpQuery让php处理html代码像jQuery一样方便
解决phpcms更换javascript的幻灯片代码调用图片问题
PHP_SELF,SCRIPT_NAME,REQUEST_URI区别
php提示Failed to write session data错误的解决方法
php运行提示:Fatal error Allowed memory size内存不足的解决方法
phpmyadmin提示The mbstring extension is missing的解决方法
php中magic_quotes_gpc对unserialize的影响分析
ThinkPHP中url隐藏入口文件后接收alipay传值的方法
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
thinkphp使用literal防止模板标签被解析的方法
php实现的Captcha验证码类实例
php实现根据字符串生成对应数组的方法
php中strstr、strrchr、substr、stristr四个函数的区别总结
PHP提示Cannot modify header information - headers already sent by解决方法
PHP-Java-Bridge使用笔记
11个PHPer必须要了解的编程规范
PHP正则替换函数preg_replace和preg_replace_callback使用总结
php使用$_POST或$_SESSION[]向js函数传参
MyEclipse常用配置图文教程
Yii框架中 find findAll 查找出制定的字段的方法对比
PHP利用MySQL保存session的实现思路及示例代码
thinkphp在模型中自动完成session赋值示例代码
CodeIgniter错误mysql_connect(): No such file or directory解决方法
Windows下的PHP 5.3.x安装 Zend Guard Loader教程
Yii使用find findAll查找出指定字段的实现方法
Yii查询生成器(Query Builder)用法实例教程
Yii中render和renderPartial的区别
PHP中echo和print的区别
浅谈php函数serialize()与unserialize()的使用方法
PHP伪静态Rewrite设置之APACHE篇