我可以显示我在 PHP 中设置的所有 cookie 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9577029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:09:43  来源:igfitidea点击:

Can I display all the cookies I set in PHP?

phpdebuggingcookies

提问by James

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?

我正在尝试诊断 cookie 中的错误,但 cookie 的名称不是它们应有的名称。有没有办法在 PHP 中打印我的域设置的所有 cookie?

回答by imm

Have you tried:

你有没有尝试过:

print_r($_COOKIE)

回答by TecBrat

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

回答by David

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

<pre><?php print_r( $_COOKIE ); ?></pre>会做你想做的。你也可以试试phpinfo()

回答by Abhi

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

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

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

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

回答by Ken Teter

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

您可以通过运行以下 php 函数来显示所有定义的 cookie:

var_dump($_COOKIE);

回答by Manish

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

回答by OXiGEN

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

与任何输入一样,安全实践应包括过滤和验证。由于所有 cookie 都是字符串,因此请清理字符串:

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

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

回答by Minuka ArTy Weerathunga

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

这将检查是否设置了任何 cookie,如果找到将遍历每个并打印出 cookie 名称和值