Can I Display All the Cookies I Set in Php?

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

8 Answers

Have you tried:

print_r($_COOKIE)
foreach ($_COOKIE as $key=>$val)
  {
    echo $key.' is '.$val."<br>\n";
  }
2

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

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

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

0

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

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

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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest