Can I display all the cookies I set in PHP?

2020-02-10 11:12发布

问题:

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?

回答1:

Have you tried:

print_r($_COOKIE)


回答2:

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


回答3:

<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



回答5:

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

var_dump($_COOKIE);


回答6:

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


回答7:

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



回答8:

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