开发者

Attempting to unserialize an array in php

开发者 https://www.devze.com 2023-02-26 06:48 出处:网络
if ($delete_one == null && $delete_two == null && $delete_three == null) { echo \"THE CONTENT SHOULD BE GRABED ONCE\";
if ($delete_one == null && $delete_two == null && $delete_three == null) {
    echo "THE CONTENT SHOULD BE GRABED ONCE";
    $arr = array("foo" => "bar", 12 => "one");
    echo $arr["foo"];
    echo $arr[12];
    $serialize_arr = serialize($arr);
    setcookie("test", $serialize_arr);

}
else
{
    echo " THIS PART WORKS! ";
    $arr_ser = $_COOKIE["test"];
    $arr = unserialize($arr_ser);
    var_dump(arr_ser);
    var_dump($arr);
    if ($arr == null) {
        echo "   Why is there nothing in here??   ";
    }
     else
    {
        echo "There is something in this array!";
    }   
}

The cookie is set when non of the $delete_one, two or three are used and when the user submits the form one of these elements change thus completing the else statement. The cookie is brought back in and the var_dump(arr_ser) will display it showing the serialized array. However the unserialize doesn't work. It is false and I am unsure why the unserialization does not work.

I've tried what Raisen said and looked through the PHP manual and tried to emulate what the examples show. I've created two if statements the second one being a replica of the example and the other is slightly changed to put other content into an array. The example works but the changes I've made does not. NOTE: I'm using this in the back-end of WordPress to create a plugin.

if ($delete_one == null && $delete_two == null && $delete_three == null) {
    $arr [foo] = "bar"; 
    $arr [boo] = "one";
    $serialize_arr = gzcompress(serialize($arr), 9);
    setcookie("test", $serialize_arr, time()+60*60*24*60, '/');

}
else
{
    if(isset($_COOKIE[test])) {
        $array = unserialize(gzuncompress($_COOKIE[test]));
        echo "<pre>";
        print_r($array);
        echo "</pre>";
    }
}   

if ($delete_one == null && $delete_two == null && $delete_three == null) {  
    $data[qwerty] = "blah";
    $data[1][a] = "1aa";
    $data[2][b] = "2bb";
    $data[3][c] = ";;;;;;";
    $data[name][first] = "Bob";
    $data[name][last] = "Jones";
    $data[val] = "This is a real long test string, to see how well the compression works!";

    $string = gzcompress(serialize($data), 9);
    setcookie("my_var", $string, time()+60*60*24*60, '/');
}
else
{
    if(isset($_COOKIE[my_var])) {
        $array = unserialize(gzuncompress($_COOKIE[my_var]));
        echo "<pre>";
        print_r($array);
    开发者_运维知识库    echo "</pre>";
    }
}   

The content doesn't unserialize thus gzuncompress has a data error.


The problem you are having is pretty simple and shown in your comment:

I've confirmed that the gzcompress was creating the gibberish. The var_dump($arr_ser); now returns string(54) "a:2:{s:3:\"foo\";s:3:\"bar\";s:3:\"boo\";s:3:\"one\";}"

you have either magic_quotes_runtime and/or magic_quotes_gps turned on. the value of the cookie has been run through AddSlashes(), so is no longer a valid serialized object. the simple fix is to either disable those options, or change this line:

$arr_ser = $_COOKIE["test"];

to this:

$arr_ser = StripSlashes($_COOKIE["test"]);
0

精彩评论

暂无评论...
验证码 换一张
取 消