开发者

Cookie is setting twice (duplicated)

I'm new to cookies, and im trying to set a cookie where to store the referrer (the org ref).

But when i try this function:

function do_it_cookie() {

// Check if cookie exists
if (isset($_COOKIE['ref'])) {

    // It dose exist, do nothing or anything... 

    } else {
        setcookie ('ref', $_SERVER['HTTP_REFERER'开发者_Python百科], time() + 60, '/');
        header ("Location: http://www.nyttforetag.com/mind-your-own-business/");
    }
}

I want to store the cookie on the user computer for 30 days, if the return i want to know the initial refereeing url.

But when i use this and lets say i go to another page in my site and then go back to the homepage its sets a new cookie with the exact same name and with the ref of the previous page.

Is there away to avoid this?


Now you cookie stores for 60 sec.

time()+60*60*24*30 //this makes store it for 30 days.

from php.net expire -

The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes)


First Point, please set the cookies name is unique for every day. Please add the cookies name with date. Set the cookie in home or first pages only. For 30 days, you can use calculation as time()+60*60*24*30.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜