I have a code that saves data to a cookie. and the data saved in the cookie is like this:
(%22Lynyrd%20Skynyrd%22%3D%3Earray(%226983887641%22)%2C%20(%22Rod%20Stewart%22%3D%3Earray(%2259088763306%22)%2C%20(%22Led%20Zeppelin%20Official%22%3D%3Earray(%22131572223581891%22)%2C%20(%22Black%20Sabbath%22%3D%3Earray(%2256848544614%22)%2C%20(%22Hadag%20Nahash%20%D7%94%D7%93%D7%92%20%D7%A0%D7%97%D7%A9%22%3D%3Earray(%22116542622632%22)%2C%20
and when i read it with this php:
<?php
if (isset($_COOKIE["currentsearchctrl"])) {
$cookz = $_COOKIE["currentsearchctrl"];
echo $cookz;
} else {
echo 'cookie not set';
} ?>
It returns with this:
(\"Lynyrd Skynyrd\"=>array(\"6983887641\"), (\"Rod Stewart\"=>array(\"59088763306\"), (\"Led Zeppelin Official\"=>array(\"131572223581891\"), (\"Black Sabbath\"=>array(\"56848544614\"), (\"Hadag Nahash הדג נחש\"=>array(\"116542622632\"),
The problem are the backslashes. I was going to: $data = array($cookz); but it doesn't work.
any ideas?
It's because magic quotes is enabled on the host. It's a horrible "feature" and everybody wishes it would just die.
If you can edit php.ini on the server, set magic_quotes_gpc = Off
and magic_quotes_runtime = Off
.
Otherwise, if Apache is the HTTP server, there is a .htaccess
trick you can use, documented here.
Otherwise, you have to use stripslashes()
to get data back to normal. Here is the include'd script I use to disable magic quotes when it isn't possible to disable it another way:
if (get_magic_quotes_gpc()) {
function array_stripslashes(&$array) {
foreach($array as $k => $v) {
if (is_array($v)) {
array_stripslashes($array[$k]);
} else {
$array[$k] = stripslashes($v);
}
}
}
array_stripslashes($_GET);
array_stripslashes($_POST);
array_stripslashes($_COOKIE);
array_stripslashes($_REQUEST);
}
set_magic_quotes_runtime(0);
%22 is HTML for " and PHP is delimiting these quotes with a backslash. See this for more info: http://aaroncameron.net/article.html?aID=59
You can turn this feature off, or you can remove slashes yourself.
You can't do it this way.
To write away a cookie use something like, write the $cookievalue to the array.
$cookievalue = implode("|", $array);
and retrieve the data, and put it back into an array:
$cookieArray = explode("|", $_COOKIE['cookie']);
most probably you have enabled magic_quotes_gpc,
as it escape double quote for GET,POST,COOKIE