dongmian5325 2013-06-05 03:00
浏览 39
已采纳

如何在PHP中正确获取Cookie的价值? [关闭]

I have code with the following segment in it. This block is actually a debug block used to check before making the script live.

if ($_COOKIE['visits'] = '' ) 
{
    $dbgmsg="No cookie found";
    $visits=1;  
    setcookie('visits',$visits,time() + (86400*30)); // 86400 = 1 day 900=15mins    
}
else 
{

    $visits=$_COOKIE['visits'];
    //$dbgmsg="Cookie found. Value is".$_COOKIE['visits'];
    $dbgmsg='<p>Cookie found. Value is'.$_COOKIE["visits"].'</p>';
    $visits++;
    setcookie('visits',$visits,time() + (86400*30));
}

When the script runs, the output is: Cookie found. Value is

Meaning the value of the cookie is not displayed. Checking the cookie value in Chrome shows that the cookie 'visits' exists, with a value 1. But the code seems to be going into the else block. But the value retrieved is null?

I wish to increment the value of the cookie depending on visits.

  • 写回答

1条回答 默认 最新

  • douzhaolu4839 2013-06-05 03:03
    关注

    You're using the assignment = operator instead of the comparison == operator. That will always be true which is not what you want.

    Change

    if ($_COOKIE['visits'] = '' ) 
    

    to

    if ($_COOKIE['visits'] == '' ) 
    

    or even better, use empty()

    if (empty($_COOKIE['visits'])) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败
  • ¥150 计算DC/DC变换器平均模型中的参数mu
  • ¥25 C语言代码,大家帮帮我
  • ¥15 请问以下文字内容及对应编码是用了什么加密算法或压缩算法呢?