简体   繁体   English

将PHP cookie转换为字符串

[英]PHP cookie into a string

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,根据http://www.w3schools.com/php/php_cookies.asp

<?php 
  print_r($_COOKIE); 
?>

But I want to do more with the content of the cookie. 但是我想用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? 有没有办法将cookie名称和值连接成一个字符串变量,而不知道cookie名称,而不是依赖于print_r?

I couldn't find answers online. 我在网上找不到答案。 Thank you in advance. 先感谢您。

Either of these may work depending on the complexity of your cookie array 根据cookie数组的复杂性,这些中的任何一个都可以工作

implode(',',$_COOKIE)

json_encode($_COOKIE)

serialize($_COOKIE)

I'd advise against this, and just rely on traversing the array 我建议不要这样做,只是依靠遍历数组

simple example: $_COOKIE['name'] 简单示例:$ _COOKIE ['name']

You can get access to all coookie names, without knowing them in advance, with code like this: 您可以在不事先了解所有coookie名称的情况下访问所有coookie名称,代码如下:

foreach ($_COOKIE as $name => $value) {
   print "Variable " . $name . " has value " . $value . "<br />\n";
}

I use this with all debuggin, _POST, _GET, _SESSION, _COOKIE 我使用它与所有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>";  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM