Jeff Davidson
Jeff Davidson

Reputation: 1929

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 expiration 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);

}

Upvotes: 0

Views: 183

Answers (3)

aleXela
aleXela

Reputation: 1301

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.

Upvotes: 0

cdhowie
cdhowie

Reputation: 169403

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.)

Upvotes: 3

MaxSantos
MaxSantos

Reputation: 156

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

Upvotes: 2

Related Questions