Ji Mun
Ji Mun

Reputation: 1830

PHP cookie into a string

I read that in PHP, you can view all the cookies by using print_r, according to http://www.w3schools.com/php/php_cookies.asp .

<?php 
  print_r($_COOKIE); 
?>

But I want to do more with the content of the cookie.

Is there a way to concatenate the cookie names and values into, something like a string variable, without knowing the cookie names, instead of relying on print_r?

I couldn't find answers online. Thank you in advance.

Upvotes: 3

Views: 5954

Answers (3)

Lonelyisland
Lonelyisland

Reputation: 51

I use this with all debuggin, _POST, _GET, _SESSION, _COOKIE

 echo "<pre>";
foreach ($_COOKIE as $key => $val) {
    echo "[$key] = $val \r\n";
    if(is_array($val)) {
        echo "is ARRAY ::::::: \"$key\" : \r\n";
        foreach ($val as $key2 => $val2) {
            echo "[$key2] = $val2 \r\n";
        }
    }
}
echo "</pre>";  

Upvotes: 0

amindfv
amindfv

Reputation: 8448

You can get access to all coookie names, without knowing them in advance, with code like this:

foreach ($_COOKIE as $name => $value) {
   print "Variable " . $name . " has value " . $value . "<br />\n";
}

Upvotes: 3

Scuzzy
Scuzzy

Reputation: 12332

Either of these may work depending on the complexity of your cookie array

implode(',',$_COOKIE)

json_encode($_COOKIE)

serialize($_COOKIE)

I'd advise against this, and just rely on traversing the array

simple example: $_COOKIE['name']

Upvotes: 6

Related Questions