3

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.

3 Answers 3

6

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']

Sign up to request clarification or add additional context in comments.

1 Comment

Traversing most likely meaning using a foreach loop.
3

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";
}

Comments

0

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>";  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.