用PHP导入Cookies(初学者问题)
我以前使用过会话,但从未使用过cookies。我想使用cookies有两个原因:用PHP导入Cookies(初学者问题),php,cookies,count,conditional,setcookie,Php,Cookies,Count,Conditional,Setcookie,我以前使用过会话,但从未使用过cookies。我想使用cookies有两个原因: 1) 这是一种新的学习方式 2) 我希望cookie在一个小时左右过期(我知道在代码示例中它在40秒内过期) 我试图写一个基本的if语句 if($counter=="1") { //do this second } elseif ($counter >="2") { //do this every time after the first and second }
1) 这是一种新的学习方式
2) 我希望cookie在一个小时左右过期(我知道在代码示例中它在40秒内过期) 我试图写一个基本的if语句
if($counter=="1") { //do this second
}
elseif ($counter >="2") { //do this every time after the first and second
}
else {// this is the first action as counter is zero
}
下面是我用来设置cookie的代码:
// if cookie doesnt exsist, set the default
if(!isset($_COOKIE["counter_cookie"])) {
$counter = setcookie("counter_cookie", 0 ,time()+40);
}
// increment it
$counter++;
// save it
setcookie("counter_cookie", $counter,time()+40);
$counter = $_COOKIE["counter_cookie"];
问题是计数器将从0设置为1,但不会从1设置为2,依此类推。任何帮助都会很好我知道这是一个非常简单愚蠢的问题:|
谢谢 问题很可能与这一行有关:
$counter = setcookie("counter_cookie", 0 ,time()+40);
看起来您希望setcookie返回一个值,但这不会发生。相反,setcookie只会在成功时返回布尔值true,在失败时返回布尔值false
您可以尝试这样重写,以达到预期效果:
if(isset($_COOKIE["counter_cookie"]))
{
$counter = $_COOKIE["counter_cookie"];
}
else
{
$counter = 0;
}
$counter++
setcookie("counter_cookie", $counter ,time()+40);
谁偷了饼干罐里的饼干?你偷了饼干罐里的饼干!谢谢,我通过使用//如果没有,则设置默认的if(!isset($\u COOKIE[“counter\u COOKIE”]){setcookie(“counter\u COOKIE”,0,time()+40);}//获取计数器$counter=$\u COOKIE[“counter\u COOKIE”]的值;你在编辑它的同时我也在编辑,本质上我的编辑做了同样的事情,它只是在设置之后得到cookie的值。然后添加它。再次感谢你的帮助