3

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);
Andreas Wong
60.7k19 gold badges112 silver badges123 bronze badges
asked Jun 6, 2012 at 2:30
6
  • @dqlopez: it's a result of print_r Commented Jun 6, 2012 at 2:31
  • Can you add a snippet of how you're actually doing this? Also, do your web server logs show anything relevant? Make sure you have error_reporting set to something high enough to report it properly, and maybe make sure display_errors is set to true. Commented Jun 6, 2012 at 2:32
  • @dqlopez it's a result of print_r :o Commented Jun 6, 2012 at 2:32
  • I tried you JSON on a validator, and it is indeed valid JSON. Commented Jun 6, 2012 at 2:34
  • added my code, what do you mean its a result of print_r? Commented Jun 6, 2012 at 2:41

2 Answers 2

12

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);
answered Jun 7, 2012 at 6:43
Sign up to request clarification or add additional context in comments.

Comments

3

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);
answered Jun 6, 2012 at 2:45

2 Comments

I get 4 = JSON_ERROR_SYNTAX when I do json_last_error(); I tried what you gave me but strip_slashes does not work did you mean stripslashes?
I tried to decode the data when it is not a cookie and it seems to decode fine but if I pull it from the cookie it does not work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.