Jump to content

Recommended Posts

I've searched the web and tried every solution known to man but the cookie steadfastly clings to life. I am so frustrated that I could scream.

 

    if (isset($_POST['logout'])) {
        unset($_SESSION);
        unset($VMemberID);
        session_destroy();
        setcookie("MLcookie_MemberID") ; // doesn't matter what this line is, the cookie is never deleted
        echo "** " . $_COOKIE["MLcookie_MemberID"] . " **";    
    }

 

I've tried:

setcookie("MLcookie_MemberID","",1);

setcookie("MLcookie_MemberID",null,1);

setcookie("MLcookie_MemberID",false,1);

setcookie("MLcookie_MemberID",$VMemberID,time()-3600);

setcookie("MLcookie_MemberID",$VMemberID,time()-3600."/");

and all variations and combinations of the above

 

My script next checks to see if $VMemberID is not null, then reads the cookie. and finds that the cookie was not deleted and the person is STILL logged in.

 

It also varies with browser.  Wih IE if the logoff button is clicked a second time the user is logged off and the cookie is erased.  With Chrome and Firefox, the user is never logged off nor cookie deleted no matter how many times logout is clicked.

 

 

help me please

 

bob

Edited by Usagi
Link to comment
https://forums.phpfreaks.com/topic/287517-deleting-cookies/
Share on other sites

You have to give the cookie a negative duration I believe.  Also - per the Manual - you can't send a cookie once you have generated output, which the echo above is probably doing for you.  Also - per the Manual - you are supposed to use all the same parameters (except expire) to delete a cookie.

 

I do this all the time with my secured apps and simply respond to a client's request to log out with a set cookie with a time()-3600 as you tried in one of your examples.  Note that the cookie will still exist in your session until the next script is called since they were loaded by the browser.  At the end of your current script the brower will delete its cookie and the next start of your session will no reflect it.

 

All this was found in the php manual - a wonderful resource!

Link to comment
https://forums.phpfreaks.com/topic/287517-deleting-cookies/#findComment-1474948
Share on other sites

You can only make them expire by setting their expiration date to a time in the past.

 

Also note when using setcookie() the affects will only take place on the next page request. This is why you should also destroy the associated $_COOKIE superglobal too,e g

setcookie("MLcookie_MemberID",$VMemberID,time()-3600); // expire the cookie
unset($_COOKIE['MLcookie_MemberID']); // and unset the cookie superglobal too

otherwise $_COOKIE['MLcookie_MemberID'] will still be set untl the next page request.

Link to comment
https://forums.phpfreaks.com/topic/287517-deleting-cookies/#findComment-1474950
Share on other sites

Also know that you have to match the existing cookie's domain and path parameters. So the checklist is:

  • Calling setcookie() before any output
  • Giving an expiration time in the past (not necessarily a negative number, just something )
  • Same domain as before
  • Same path as before
Link to comment
https://forums.phpfreaks.com/topic/287517-deleting-cookies/#findComment-1474967
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.