0

I am outputting to my error log with this code:

ob_start();
var_dump($_COOKIE['agl-values']);
error_log(ob_get_clean());

The output is:

[02-Apr-2018 16:12:58 UTC] string(321) "{\"latitude\":\"42.2470259\",\"longitude\":\"-71.1755274\",\"altitude\":\"NaN\",\"accuracy\":\"29\",\"altitudeAccuracy\":\"NaN\",\"heading\":\"NaN\",\"speed\":\"NaN\",\"error_code\":\"\",\"error_message\":\"\",\"php_time\":1522684274,\"php_date\":\"2018-04-02 15:51:14\",\"php_date_format\":\"Y-m-d H:i:s\",\"user_id\":0}"

[02-Apr-2018 16:12:58 UTC] string(321) "{\"latitude\":\"42.2470259\",\"longitude\":\"-71.1755274\",\"altitude\":\"NaN\",\"accuracy\":\"29\",\"altitudeAccuracy\":\"NaN\",\"heading\":\"NaN\",\"speed\":\"NaN\",\"error_code\":\"\",\"error_message\":\"\",\"php_time\":1522684274,\"php_date\":\"2018-04-02 15:51:14\",\"php_date_format\":\"Y-m-d H:i:s\",\"user_id\":0}"

[02-Apr-2018 16:12:58 UTC] string(321) "{\"latitude\":\"42.2470259\",\"longitude\":\"-71.1755274\",\"altitude\":\"NaN\",\"accuracy\":\"29\",\"altitudeAccuracy\":\"NaN\",\"heading\":\"NaN\",\"speed\":\"NaN\",\"error_code\":\"\",\"error_message\":\"\",\"php_time\":1522684274,\"php_date\":\"2018-04-02 15:51:14\",\"php_date_format\":\"Y-m-d H:i:s\",\"user_id\":0}"

[02-Apr-2018 16:12:58 UTC] string(321) "{\"latitude\":\"42.2470259\",\"longitude\":\"-71.1755274\",\"altitude\":\"NaN\",\"accuracy\":\"29\",\"altitudeAccuracy\":\"NaN\",\"heading\":\"NaN\",\"speed\":\"NaN\",\"error_code\":\"\",\"error_message\":\"\",\"php_time\":1522684274,\"php_date\":\"2018-04-02 15:51:14\",\"php_date_format\":\"Y-m-d H:i:s\",\"user_id\":0}"

I just need to access the latitude and longitude variables. How do I isolate one of the cookies, and then how do I turn it into JSON?

UPDATE

I checked json_last_error() and it is telling me the string has a syntax error - could it be the escaped double quotes?

ewizard
  • 2,657
  • 4
  • 49
  • 101
  • Try json decoding the string you get from `$_COOKIE['agl-values']` like so: `json_decode($_COOKIE['agl-values'])['latitude']`? – Ethan Apr 02 '18 at 16:19
  • `var_dump(json_decode($_COOKIE['agl-values'])['latitude']);` returns `NULL` – ewizard Apr 02 '18 at 16:20
  • See https://stackoverflow.com/questions/2348152/detect-bad-json-data-in-php-json-decode to check if the json is invalid. Use the `json_last_error()` function to find the specific error. – Ethan Apr 02 '18 at 16:21
  • I figured out the `json_last_error()` and it says there is a syntax error – ewizard Apr 02 '18 at 16:31
  • aren't there supposed to be single quotes in a json string? – ewizard Apr 02 '18 at 16:32
  • I think it's because of the `\"`s. Try doing a `str_replace` to filter them out: `json_decode(str_replace('\"', '"', $_COOKIE['agl-values']))` – Ethan Apr 02 '18 at 16:36
  • 1
    thanks @David that works - please provide an answer and i'll mark it correct – ewizard Apr 02 '18 at 16:42

3 Answers3

1

Looks like there's some escaped quotes in the json (\") which means that the json_decode would fail with a syntax error. Try using str_replace to change them into regular quotes ("):

json_decode(str_replace('\"', '"', $_COOKIE['agl-values']))

Then, to get the latitude and longitude, do something like this:

$aglValues = json_decode(str_replace('\"', '"', $_COOKIE['agl-values']));

var_dump($aglValues->latitude);
var_dump($aglValues->longitude);
Ethan
  • 4,165
  • 4
  • 25
  • 43
0

Looks like it’s stored as a string. Convert to a JSON object then reference:

$value = json_decode($_COOKIE['agl-values']);
echo $value->longitude . ' ' . $value->latitude;

Update

Here's a direct sample from your string:

php > $var = json_decode("{\"latitude\":\"42.2470259\",\"longitude\":\"-71.1755274\",\"altitude\":\"NaN\",\"accuracy\":\"29\",\"altitudeAccuracy\":\"NaN\",\"heading\":\"NaN\",\"speed\":\"NaN\",\"error_code\":\"\",\"error_message\":\"\",\"php_time\":1522684274,\"php_date\":\"2018-04-02 15:51:14\",\"php_date_format\":\"Y-m-d H:i:s\",\"user_id\":0}");
php > echo $var->longitude;
-71.1755274
Will Fitch
  • 96
  • 3
0

The cookie is already in JSON format, you need to decode it properly.

Use:

print_r(json_decode($_COOKIE['agl-values'], true));

Or for a single value:

echo json_decode($_COOKIE['agl-values'], true)['latitude'];

To use default json decode object:

echo json_decode($_COOKIE['agl-values'])->latitude;
Xorifelse
  • 7,708
  • 1
  • 24
  • 37
  • the bottom two return null, i dont need to print – ewizard Apr 02 '18 at 16:29
  • @ewizard Not according to https://3v4l.org/7Y75n , there was a issue with the the last line, but fixed in the update. I copyed the string directly from your error log. – Xorifelse Apr 02 '18 at 16:37
  • yah....that appears to work...but when i use the cookie variable with the string in it...it doesn't – ewizard Apr 02 '18 at 16:40
  • @ewizard Odd, what version of PHP are you running? According to the comments it is `\"` being someone escaped incorrectly. The suggested "fix" is to replace it with just `"`. However I would call that a patch as nothing is truely fixed and issues might arise in the future. What code causes the json generation in the first place? – Xorifelse Apr 03 '18 at 07:03