how to automatically logout when close the browser in php?

Posted on 2006-03-25
Last Modified: 2012-06-21
I would like my user to automatically logout when he's closing his browser.  How can I do that.

Question by:vn4phuong
    LVL 33

    Accepted Solution

    User will auto logout when he closes his browser. Session will be lost.

    Author Comment

    i am using cookies
    LVL 33

    Expert Comment

    Cookies will also be expired.
    By default, it will kill itself when browser is closed unless you specify it to specify at a specified number of days later.

    LVL 12

    Expert Comment

    Why cookies?  My temporary internet files folder is already cluttered with all those little images you force me to download.

    ~Happy User

    ...sorry, just looking for interesting conversation.  Seems like the honorable hongjun has this one under control.  As an option you can manually kill the session, but as was stated above, it's really not an issue, I have however heard that when many of the larger banks delete financial records from the disk, they write the physical addresses to binary zeros seven times, what's the point of that?  Anyway, in jsp, I'm fairly sure it's:


    ...but, unfortunatly, my memory isn't what it used to be so there may be other scenarios to consider, here's a link that says you should unregester your variables before doing so, uh I dunno if that's actually necessary



    Author Comment

    function do_whatever()

           delete_cookie ( "viet1800" );


    function delete_cookie ( cookie_name )
      var cookie_date = new Date ( );  // current date & time
      cookie_date.setTime ( cookie_date.getTime() - 1 );
      document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();

    <body onUnload="do_whatever()">


    how come this code doesn't delete the cookies?

    LVL 6

    Expert Comment

    you need to set the cookie time to -1 -- which means cookie is deleted when the browser window is closed!
    secondly, have an onunload handler on your document as suggested above which calls the function that you want to expire the session.
    LVL 1

    Expert Comment

    The Unload event only fires immediately before an object is unloaded. Objects are not unloaded when you close your browser window. The BODY Unload event would fire when you browse off to another page.

    I suggest you do what is being suggested by 'liviutudor' and set the cookie to expire when the browser session is over (when all browser windows are closed).

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to count occurrences of each item in an array.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now