开发者

Cookie deleting

What I'm wanting to do is for the remember me checkbox. I have it set up to where if there is a cookie set for the username then it checks the checkbox. What I'm wanting to do is if there was a cookie but the user decides to uncheck it just in case someone else wants to access their account from the same computer then it'll delete the cookie I"m not sure how to accomplish this. Here's what I have so far.

if (isset($_POST['remember'])) {

    // Sets an 开发者_如何学Goexpiration time for the cookie
    $my_expiration = time()+60*60*24*100;

    // Sets the cookie for the username
    setcookie("username", $username, $my_exiration, "/");

} else {

    setcookie ("username", "", time() - 3600);

}


This will work if you add the path ("/") to the second setcookie() call. Since you are omitting that, the browser is treating the cookie as a different one than the previously-set cookie, and will therefore not delete it:

setcookie ("username", "", time() - 3600, "/");

(At least I assume that's what's going wrong. You didn't actually ask a question, you just sort of threw code up there and said "I'm doing this" without indicating if anything isn't working as you expect.)


Set it to null setcookie("username", null, 0, "/");


Both setting and deleting must have path

setcookie("ST",$_COOKIE['ST'],time()+1000,'/'); //for creation
setcookie('ST',NULL,-1,'/'); //for deletion

I played with this until get it done. Hope it useful.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜