I have an array like this:
Array (
[utm_source] => website
[utm_medium] => fbshare
[utm_campaign] => camp1
[test_cat] => red
[test_sub] => Category
[test_ref] => rjdepe
)
which I json_encode and put into a cookie. I take it from the cookie and now want to decode it but I get a blank screen. I am confused as to what is wrong. To me this JSON looks correct:
{"utm_source":"website","utm_medium":"fbshare","utm_campaign":"camp1","test_cat":"red","test_sub":"Category","test_ref":"dodere"}
Any ideas?
Edit:
My code:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
$value = json_encode($value);
setcookie("TestCookie", $value, time()+3600);
Other Page:
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode($cookie);
print_r($cookie);
2 Answers 2
Try base64_encoding it like such:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
$value = base64_encode(json_encode($value));
setcookie("TestCookie", $value, time()+3600);
Other Page:
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode(base64_decode($cookie));
print_r($cookie);
Comments
Before your:
print_r($cookie);
Do:
json_last_error();
Does it return anything? If you are getting a blank screen, it may be because the parser is failing, likely the results of the "'s in the json string within the cookie being escaped \".
Try:
$cookie = json_decode(stripslashes($_COOKIE['TestCookie']));
Update
So I used the following code, and received the following output:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
var_dump($value);
setcookie('TestCookie', json_encode($value), time()+86400);
echo $_COOKIE['TestCookie'];
print_r(json_decode($_COOKIE['TestCookie']));
Output
array(6) {
["utm_source"]=>
string(7) "website"
["utm_medium"]=>
string(7) "fbshare"
["utm_campaign"]=>
string(5) "camp1"
["test_cat"]=>
string(3) "red"
["test_sub"]=>
string(8) "Category"
["test_ref"]=>
string(6) "rjdepe"
}
{
"utm_source":"website",
"utm_medium":"fbshare",
"utm_campaign":"camp1",
"test_cat":"red",
"test_sub":"Category",
"test_ref":"rjdepe"
}
stdClass Object
(
[utm_source] => website
[utm_medium] => fbshare
[utm_campaign] => camp1
[test_cat] => red
[test_sub] => Category
[test_ref] => rjdepe
)
If you notice, encoded is an array. The json string is a string. The decoded string is an object.
You can type cast this to an array:
$value = (array) json_decode($_COOKIE['TestCookie']);
// Or
$value = json_decode($_COOKIE['TestCookie'], true);
Also,
Depending on your configuration, PHP may escape special characters in your cookie, which seems to be what your JSON decode error is relaying.
Try doing:
json_decode(str_replace('\"', '"', $_COOKIE['TestCookie']), true);
2 Comments
strip_slashes does not work did you mean stripslashes?
print_rprint_r:o