我有一些代码:
<?php
$cookiename1 = "uservalue";
$cookie1 = (string)$_COOKIE[$cookiename1];
if($cookie1 != ""){
echo "<span style = 'color: white'>". $cookie1 . "</span>";
}
else{
die("Could not get profile");
}
?>
应该检测cookie是否为空,但是当cookie等于&#34;&#34;然后它运行echo语句。
我最初有:
<?php
$cookiename1 = "uservalue";
$cookie1 = $_COOKIE[$cookiename1];
if(isset($cookie1) && !empty($cookie1)){
echo "<span style = 'color: white'>". $cookie1 . "</span>";
}
else{
die("Could not get profile");
}
?>
但我得到了同样的结果。不知道我在这里做错了什么= /如果有人可以提供帮助那就太棒了。
答案 0 :(得分:2)
您需要使用setcookie()
示例:强>
setcookie("uservalue", "some-value", time()+3600, "/", "example.com", 1);
设置cookie后,您可以检查它是否为空:
if (!empty($_COOKIE["uservalue"])){
echo "<span style='color: white'> cookie isn't empty </span>";
}
在下列情况下, empty
为true
:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)