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.

有帮助吗?

解决方案

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'])) 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top