php 设置和读取数组cookie的方法,多个cookie值处理办法
基于PHP创建Cookie数组的详解_PHP教程
setcookie("ICNet[Reg]", "This is reg cookie", time() + 3600);
setcookie("ICNet[Active]", "This is login cookie.", time() + 3600);
print_r($_COOKIE);
/*Result:
Array ( [ICNet] => Array ( [Reg] => This is reg cookie [Active] => This is login cookie ) [PHPSESSID] => nsi2q5upuvqrik6notaohjd7p3 )
*/
读取:
echo $_COOKIE['ICNet']['Reg'];
echo $_COOKIE['ICNet']['Active'];
//或者
$arr_cookie = $_COOKIE['ICNet'];
echo $arr_cookie['Reg'];
echo $arr_cookie['Active'];
销毁:
setcookie('ICNet[Reg]', '', time() - 3600);
setcookie('ICNet[Active]', '', time() - 3600);
-------------------------------
创建一个数组 cookie:
<?php
setcookie("cookie[three]","cookiethree");
setcookie("cookie[two]","cookietwo");
setcookie("cookie[one]","cookieone");
// 输出 cookie (在重载页面后)
if (isset($_COOKIE["cookie"]))
{
foreach ($_COOKIE["cookie"] as $name => $value)
{
echo "$name : $value <br />";
}
}
?>
<html>
<body>
...
...
输出:
three : cookiethree
two : cookietwo
one : cookieone