technopeasant
technopeasant

Reputation: 7939

PHP decode JSON from cookie

I'm trying to parse a JSON encoded string from a cookie and when I run json_decode() on the string it returns as null. It should be a simple operation - what am I missing?

/* Get */

    $cookie_exampleData = $_COOKIE['exmaple_data'];

    // Retrieves: '{\"FirstName\":\"Angus\",\"LastName\":\"MacGyver\",\"Email\":\"[email protected]\",\"Phone\":\"8185555555\"}'

/* Decode */

    $cookie_exampleData_decoded = json_decode($cookie_exampleData);

/* Print */

    var_dump($cookie_exampleData_decoded);

    // Returns: NULL

Upvotes: 8

Views: 11882

Answers (1)

voodoo417
voodoo417

Reputation: 12101

In this case, you need remove escaped quotes:

$cookie_exampleData = stripslashes($_COOKIE['exmaple_data']);

See stripslashes

Upvotes: 20

Related Questions