Howdy, I'm trying to write a website that allows people to login via a cookie that will expire when they close the browser. I was under the impression that if you don't include an expiration time when you set the cookie that it will automatically expire when the browser window is closed. Here is what I've got:

if (mysql_num_rows($result)==1 ) { // if name matches password
setcookie("loggedid", $userid);

header ("location: index.php");
exit;
}

and index.php tests for the cookie and displays 'Welcome...' if they are logged in...anyway, if i open index.php it doesn't say welcome, then if i log in, it goes to the index.php and i see 'Welcome...', so the cookie has been set. However, if I close the browser window after that and then reopen it and go to index.php, it still displays 'Welcome...' and i am still logged in. Why? FYI I don't have PHP on my computer, I have to upload it to my server; maybe it is something in their php.ini settings

    You're right, the cookie should not be remembered after you shut your browser. I imagine as you say it's something to do with server settings.

    I guess its better in this case to either specify times for the cookie duration or use another method such as registering session variables and destroying session on log-out.

    Dan

      The cookie is destroyed when you close ALL instances of the browser. Close not only the window you logged in with, but all your other open windows also, and you should not see that behavior.

      If you retain the cookie after closing all your windows, it is a system or a browser issue. Not a server issue.

        Thanks guys, I didn't know you had to close all windows of the browser.

          3 years later

          Hi,

          Can you please tell me how to make a cookie that expires on window closing of internet explorer?

          I need to set up that cookie for punbb board.

          Thanking for help in advance,

          Jayesh

            Write a Reply...