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'])) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度