In this paper, the effect of MAGIC_QUOTES_GPC on unserialize in PHP is analyzed. Share to everyone for your reference. Specifically as follows:
MAGIC_QUOTES_GPC is a PHP function that adds some security filtering to a single double quote, but this function has some effect on our use of the unserialize function, so let's take a look at some examples and solutions for this problem.
Yesterday friend let me help him to solve the problem of the shopping cart program of his website, the program uses is PHPCMS, change space before is good, just change the space, the specific question is the prompt to join the shopping cart to jump to the shopping cart page, the shopping cart is empty.
I looked at the code, the general principle is to store the product ID and quantity in the array, and then serialized into a cookie, in the shopping cart page deserialization cookies, get the array and read the corresponding product information.
Debug to find the problem appeared on the Unserialize, I first based on its shopping cart principle to write a section of code, the code is as follows:
Copy Code code as follows:
<?php
Header ("content-type:text/html; Charset=utf-8 ");
$magic = GET_MAGIC_QUOTES_GPC ()? "Open": "Not open";
$str = Array (' goods_id ' =>13, ' number ' =>1);
Setcookie ("Cart", serialize ($STR));
echo "MAGIC_QUOTES_GPC:". $magic. " <br> ";
echo $_cookie[' cart ']. " <br> ";
Print_r (unserialize ($_cookie[' cart '));
?>
This code can be found that when your MAGIC_QUOTES_GPC is closed, the program executes without any problems, but when the MAGIC_QUOTES_GPC is turned on, you will find that deserialization is not successful, then you may know what the problem is?
The reason is that when MAGIC_QUOTES_GPC is turned on, the system automatically escapes the single quotation mark in the result of the post get COOKIE plus \, so the value of $_cookie[' cart ' becomes a:1:{i:0;a:2:{s:8:\ "Goods_ Id\ "; i:13;s:6:\" Number\ "; I:1}}, in which case unserialize is unable to deserialize successfully, there is a problem.
The solution is simply to convert unserialize ($_cookie[' cart ') to Unserialize (stripslashes ($_cookie[' cart ')) and add stripslashes before the COOKIE, Remove the escape character, so that's no problem.
For cookies, we'll do another test:
1. Problem: The project data needs to be serialized and stored in a cookie, then the cookie's data counter sequence is obtained, and the code is as follows:
Copy Code code as follows:
$a [0] = Array ("Key" => "Ha Luo");
$a [1] = Array ("Key" => "Ha Luo");
$jsona = Json_encode ($a);
Setcookie ("TestCookie", "");
Setcookie ("TestCookie", $jsona);
Var_dump ($jsona, true); Normal value
Var_dump (Json_decode ($_cookie[' TestCookie '],true)); Value not taken
When the cookie is not assigned, the normal deserialization, after the cookie, take the value is null.
2. Analysis, the code is as follows:
Copy Code code as follows:
$a [0] = Array ("Key" => "Ha Luo");
$a [1] = Array ("Key" => "Ha Luo");
$jsona = Json_encode ($a);
Var_dump ($jsona); string [{' key ': ' \u54c8 \u903b '},{' key ': ' \u54c8 \u903b '}] '
Setcookie ("TestCookie", "");
Setcookie ("TestCookie", $jsona);
Var_dump ($_cookie[' TestCookie ')); String ("{\" key\ ": \" \\u54c8 \\u903b\ "},{\" key\ ": \" \\u54c8 \\u903b\ "}]"
Var_dump (Json_decode ($_cookie[' TestCookie '],true));
Compared to the data, after the cookie processing a few more//, the solution, the code is as follows:
Copy Code code as follows:
Var_dump (Json_decode (stripslashes ($_cookie[' TestCookie ')), true);
Var_dump (Json_decode (Str_replace ("\", "", $_cookie[' TestCookie '), true));
3. Summary:MAGIC_QUOTES_GPC in the case of opening, it will affect the data obtained through get|post|cookies. So when we are get|post|cookies processing the data, we first judge the MAGIC_QUOTES_GPC opening.
① in the open case, processing the data requires stripslashes
② in the absence of open, accept the data first addslashes, processing data stripslashes
I hope this article will help you with your PHP program design.