34

I am trying to diagnose an error in my cookies, but the names of the cookies are not what they should be. Is there a way in PHP to print all the cookies that have been set by my domain?

2
  • You can try echo json_encode($_COOKIE); for printing all cookies neatly as a json. Feb 10, 2017 at 7:15
  • +1 upvote for question and answer - useful as the basis for writing own cookie notice banner for GDPR purposes. Oct 29, 2018 at 12:02

8 Answers 8

67

Have you tried:

print_r($_COOKIE)
32
foreach ($_COOKIE as $key=>$val)
  {
    echo $key.' is '.$val."<br>\n";
  }
2
  • Hi, this only shows cookies set by me only, what about other cookies (which other websites i,e amazon etc saves in my browser). Please respond
    – Usman
    Jul 2, 2017 at 12:25
  • 1
    @Usman That is beyond the scope of the question that was asked. Cookies set by other sites will not be available to your PHP script. If you're asking as a user, try asking in Super User Be sure to specify your OS and Browser.
    – TecBrat
    Jul 2, 2017 at 19:42
5

<pre><?php print_r( $_COOKIE ); ?></pre> will do what you want. You might also try phpinfo().

4

echo $_COOKIE["cookie_name"]; // Print an individual cookie

print_r($_COOKIE); // Another way to debug/test is to view all cookies

0
2

You can display all cookies defined by running the following php function:

var_dump($_COOKIE);
1
if($_COOKIE) {
  print_r($_COOKIE);     //print all cookie
}
else
{
   echo "COOKIE is not set";    
}
1

As with any inputs, security practices should include filtering and validation. Since all cookies are strings, sanitize the strings:

var_dump(filter_input_array(INPUT_COOKIE, FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY))

PHP Docs: https://www.php.net/manual/en/function.filter-input-array.php

0
if($_COOKIE) {
   foreach ($_COOKIE as $key=>$val)
   {
       echo $key.' is '.$val."<br>\n";
   }
}
else
{
    echo "No Cookies are Set";    
}

This will check if any cookies are set, if found will iterate through each and print out the cookie name and value

1
  • And to access only cookies that I've set manually and to ignore the rest, One approach that you can use is to put all keys into an array in your Php and iterate through $_COOKIE list and check if the keys match and ignore if matched. Nov 18, 2019 at 10:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.