2010-04-24 82 views
0

我以前,但从来没有使用的Cookie的会话。我想使用cookie有两个原因:
1)这是一些新的学习
2)我想有一个小时左右的cookie过期(我知道它的代码示例过期40秒)Incrmenting用PHP饼干(初级问题)

我想写一个基本的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"]; 

的问题是,反会SE t从0到1,但不会从1到2,等等。任何帮助将是伟大的我知道这是一个非常简单的愚蠢问题:|

谢谢!

+0

谁从饼干罐里面偷走了饼干? – Rob 2010-04-24 03:31:53

+0

你偷饼干的饼干罐! – BandonRandon 2010-04-24 03:35:00

回答

1

该问题最有可能与这条线:

$counter = setcookie("counter_cookie", 0 ,time()+40); 

看样子你期待的setcookie返回一个值,但这是不会发生的。相反,setcookie只会在成功时返回布尔值true,在失败时返回false。

http://php.net/manual/en/function.setcookie.php

你可以尝试重写它像这样以达到预期的效果:

if(isset($_COOKIE["counter_cookie"])) 
{ 
    $counter = $_COOKIE["counter_cookie"]; 
} 
else 
{ 
    $counter = 0; 
} 
$counter++ 
setcookie("counter_cookie", $counter ,time()+40); 
+0

感谢我得到它的工作通过使用 //如果它不,设置默认 \t如果(!isset($ _ COOKIE [ “counter_cookie”])){ \t \t的setcookie( “counter_cookie”,0,时间()+ 40); \t \t \t \t } \t //获取计数器的值 \t $计数器= $ _COOKIE [ “counter_cookie”]; – BandonRandon 2010-04-24 03:40:11

+0

你是在同一时间,我是编辑它,基本上是我的编辑做它被设置后,刚拿到cookie的值同样的事情。然后添加它。再次感谢你的帮助 – BandonRandon 2010-04-24 03:43:05