Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 430
technopeasant Avatar asked Jun 08 '26 13:06

technopeasant


1 Answers

In this case, you need remove escaped quotes:

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

See stripslashes

like image 184
voodoo417 Avatar answered Jun 11 '26 03:06

voodoo417