0

I am setting a cookie and that is working fine but for some reason I'm not able to retrieve the value of the cookie. I can see the cookie is set in the browser developer tools but when I try to print_r it comes up empty.

Here is where I see the values of the cookie in developer tools.

Browser shows the cookie is set and has values

Here is where I'm trying to get the value that is currently not working:

$additionalData = json_decode($_COOKIE['antennasNow'], true);
echo '<pre>';
print_r($additionalData);
echo '</pre>';

Here is where I set the cookie in case it is helpful:

setcookie('antennasNow', json_encode($cookieValue), time()+3600);

And this is how I'm setting the value of the cookie in case it helps:

$cookieValue = array(
        'base_Sku' => $base_Sku,
        'vhf_UHF_Type' => $vhf_UHF_Type,
        'ptc_Type' => $ptc_Type,
        'type_700_800_900' => $type_700_800_900,
        'band' => $band,
        'polarization' => $polarization,
        'gain_Sku' => $gain_Sku,
        'exposed_Dipole_Az_Pattern' => $exposed_Dipole_Az_Pattern,
        'collinear_Az_Pattern' => $collinear_Az_Pattern,
        'panel_Az_Pattern' => $panel_Az_Pattern,
        'dual_Input' => $dual_Input,
        'narrowband_Connector' => $narrowband_Connector,
        'beamtilt' => $beamtilt,
        'null_Fill' => $null_Fill,
        'heavy_Duty' => $heavy_Duty,
        'invert_Mount' => $invert_Mount,
    );

I followed these tips/steps from other threads:

PHP decode JSON from cookie

json_decode to array

Storing PHP arrays in cookies

Derek
  • 4,747
  • 7
  • 44
  • 79
  • If you `print_r($_COOKIE['antennasNow'])` directly (without attempting to decode first) do you see the raw data? – ADyson Apr 12 '23 at 15:20
  • @ADyson yes, it prints this `{\"base_Sku\":\"ATC-G\",\"vhf_UHF_Type\":\"D\",\"ptc_Type\":\"\",\"type_700_800_900\":\"\",\"band\":\"1\",\"polarization\":\"V\",\"gain_Sku\":\"2\",\"exposed_Dipole_Az_Pattern\":\"D\",\"collinear_Az_Pattern\":\"\",\"panel_Az_Pattern\":\"\",\"dual_Input\":\"D2\",\"narrowband_Connector\":\"D7M\",\"beamtilt\":\"1\",\"null_Fill\":\"NF\",\"heavy_Duty\":\"HD\",\"invert_Mount\":\"INV\"}` – Derek Apr 12 '23 at 15:23
  • Hm, that looks like the data has somehow been escaped / double-encoded at some point, meaning it's no longer valid JSON and can't be decoded. All those backslashes need to go. – ADyson Apr 12 '23 at 15:30
  • @ADyson thank you! I tried `stripslashes` previously but it didn't work but I now do `$jsonData = stripslashes($_COOKIE['antennasNow']);` and `$additionalData = json_decode($jsonData, true);` and now I have access to the data stored in `$additionalData` – Derek Apr 12 '23 at 15:47
  • That papers over the crack, but you could really do with finding out how it got corrupted in the first place. – ADyson Apr 12 '23 at 15:47

1 Answers1

0

Just replace your debug codes with this -

$additionalData = json_decode(stripslashes($_COOKIE['antennasNow']), true);
    echo '<pre>';
    print_r($additionalData);
    echo '</pre>';
itzmekhokan
  • 2,597
  • 2
  • 9
  • 9