简体   繁体   English

为什么PHP中的反序列化会继续返回false?

[英]Why does unserialize in PHP keep returning false?

I've just written the easiest script in the world, but still I can't get it to work, and it's mighty strange. 我刚刚编写了世界上最简单的脚本,但我仍然无法让它工作,而且它非常奇怪。

I want to use jQuery to catch some input field values and serialize them with jQuery's serialize() . 我想使用jQuery捕获一些输入字段值并使用jQuery的serialize()序列化它们。 I then send the serialized string to the server to unserialize it. 然后我将序列化的字符串发送到服务器以反序列化它。 Here's the output I get from the serializing in jQuery, this is what I send to the server. 这是我从jQuery中序列化得到的输出,这是我发送给服务器的。

field1=value1&field2=value2&field3=value3

And here's the function, 这是功能,

public function unserialize_input()
{
    $str = $this->input->post("user_values");
    $unserialized = unserialize($str);
    var_dump($unserialized);
}

As I said, if I go "echo $str;" 正如我所说,如果我去“echo $ str;” I get "field1=value1&field2=value2&field3=value3", so the string should be unserializable. 我得到“field1 = value1&field2 = value2&field3 = value3”,因此该字符串应该是不可序列化的。 However, I always get the same error message and the var_dump($unserialized); 但是,我总是得到相同的错误消息和var_dump($unserialized); always returns bool(false). 总是返回bool(false)。

Here's the error message I get from CodeIgniter , the framework I'm using for PHP. 这是我从CodeIgniter获得的错误消息,我正在使用PHP的框架。

Severity: Notice
Message: unserialize() [<ahref='function.unserialize'>function.unserialize</a>]: Error at offset 0 of 41 bytes

bool(false) 

I'm using MAMP and run this locally at the moment. 我现在正在使用MAMP并在本地运行。 I read something about magic_quotes_gpc being OFF could cause this locally, but it's enabled. 我读到一些关于magic_quotes_gpc被关闭的东西可能导致本地,但它已启用。 What might be wrong? 可能有什么问题?

You're using the wrong PHP function. 您正在使用错误的PHP函数。 You should use parse_str instead. 您应该使用parse_str

 parse_str($str, $unserialized);

PHP's serialize and unserialize destruct and construct PHP objects/arrays/values. PHP的序列化和反序列化破坏和构造PHP对象/数组/值。

jQuery serialize serializes a form into a POST string which can be very handy to do Ajax calls on. jQuery serialize将表单序列化为POST字符串,这对于进行Ajax调用非常方便。 A post string is not a valid serialized string in PHP and cannot be reconstructed to a PHP mixed value and thus it returns false. post字符串不是PHP中的有效序列化字符串,无法重构为PHP混合值,因此返回false。

This is going to be a bit vague because I don't know jQuery overly well, but could it be that jQuery serialize's strings even slightly different from PHP? 这有点模糊,因为我不太了解jQuery,但可能是jQuery serialize的字符串甚至与PHP略有不同? If so, then that would cause the error message that you see. 如果是这样,那么这将导致您看到的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM