Home  >  Q&A  >  body text

Get value from cookie using json_decode

I'm setting a cookie and it works fine, but for some reason I can't retrieve the cookie's value. I can see that the cookie is set in the browser developer tools, but when I try to print_r it shows up as empty.

This is where I see the cookie value in the developer tools.

This is where I'm trying to get the value which currently doesn't work:

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

This is where I set the cookie, in case it helps:

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

This is how I set the cookie value, 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

P粉235202573P粉235202573209 days ago335

reply all(1)I'll reply

  • P粉835428659

    P粉8354286592024-02-27 16:42:50

    Just replace your debug code with this -

    $additionalData = json_decode(stripslashes($_COOKIE['antennasNow']), true);
        echo '
    ';
        print_r($additionalData);
        echo '
    ';

    reply
    0
  • Cancelreply