我有一些代码:
<?php
$cookiename1 = "uservalue";
$cookie1 = (string)$_COOKIE[$cookiename1];
if($cookie1 != ""){
echo "<span style = 'color: white'>". $cookie1 . "</span>";
}
else{
die("Could not get profile");
}
?>
它应该检测cookie是否为空,但当cookie等于"时,它会运行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");
}
?>
但我得到了同样的结果。不确定我在这里做错了什么//如果有人能帮忙,那就太棒了。
您需要使用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)