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

报告相同问题?

悬赏问题

  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐