PHP 反序列化 - 索引错误
我在使用 PHP serialize
和 unserialize
函数时遇到一些困难。我正在使用 serialize(array)
将数组插入 MySQL DB。这是我在数据库中的值
a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}
如果我这样做,
$images = unserialize( $apartment->images );
我会得到错误:ErrorException [Notice]:unserialize() [function.unserialize]:Error at offset 9 of 208 bytes
。 $apartment
是我的 DB ORM 对象,如果我执行 echo $apartment->images
之类的操作,它将向我输出上面的序列化字符串。
但如果我做这样的事情
$images = unserialize( 'a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}' );
它会起作用......嗯? $apartment->images 的 var_dump() 返回
string(208) "a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}"
I am having some difficulties with PHP serialize
and unserialize
functions. I am inserting array into MySQL DB with serialize(array)
. This is my value in DB
a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}
If I do something like this
$images = unserialize( $apartment->images );
I will get error: ErrorException [ Notice ]: unserialize() [function.unserialize]: Error at offset 9 of 208 bytes
. $apartment
is my DB ORM object and if I do something like echo $apartment->images
it will output me the above serialized string.
But if I do something like this
$images = unserialize( 'a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}' );
It will work... Huh? var_dump() of $apartment->images returns
string(208) "a:4:{i:0;s:29:"k44L1b_bisnode.com.xlarge.png";i:1;s:28:"q44L1b_bisnode.com.large.png";i:2;s:29:"q44L1b_bisnode.com.medium.png";i:3;s:28:"q44L1b_bisnode.com.small.png";}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 FuelPHP 的一个人注意到了这个 http://docs.fuelphp.com/general/views .html#/security 所以这是我的问题。
安全
默认情况下,视图使用输出编码来清理您使用 Security::htmlentities() 传递给它们的任何内容。如果你想传递未经过滤的内容,可以使用方法 set($name, $value, false)。
A guy from FuelPHP noted on this http://docs.fuelphp.com/general/views.html#/security so this was my issue.
Security
By default, Views use output encoding to sanitize anything you pass to them using Security::htmlentities(). If you want to pass something unfiltered, you can use the method set($name, $value, false).