Results 1 to 14 of 14

Thread: Destroy Session When Closing The Current Window

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Destroy Session When Closing The Current Window

    Hi ,

    We can destroy The session by Going to another page set it to session_destroy() working no problem
    but sometime the user may not click on logout it may close the window
    so i want to add this event when he close the window the session will be destroyed !
    i just think that we have to do it onUnLoad Event using JavaScript but Tried with no luck !

    Thanks !

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Destroy Session When Closing The Current Window

    Unless you use the XMLHTTPRequest object you cannot do it with Javascript. PHP includes a built in mechanism to dispose of stale sessions after a period of time has elapsed. Haves a look at the session.gc_maxlifetime, session.gc_probability, session.gc_divisor directives.

    For examples when:

    session.gc_divisor = 10
    session.gc_maxlifetime = 300
    session.gc_probability = 1

    Sessions will be destroyed when active fo 5 mins (200 seconds) and the garbage collection (destruction) of these sessions will occur in 1 out of every 10 requests.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Destroy Session When Closing The Current Window

    By default, the session cookie is destroyed when the browser window is closed, which effectively logs out the user.

  4. #4
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149

    Re: Destroy Session When Closing The Current Window

    You can unset the $_SESSION variable but that only removes all the elements of the array.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Destroy Session When Closing The Current Window

    Quote Originally Posted by JRSofty
    You can unset the $_SESSION variable but that only removes all the elements of the array.
    That gets deleted at the end of each request execution anyway.

  6. #6
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149

    Re: Destroy Session When Closing The Current Window

    Only if there is no session cookie. If there is a session cookie then that variable is reloaded for each page from the session handler built into PHP. It is the one way to pass information from one page to another.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Destroy Session When Closing The Current Window

    Quote Originally Posted by visualAd
    Unless you use the XMLHTTPRequest object you cannot do it with Javascript. PHP includes a built in mechanism to dispose of stale sessions after a period of time has elapsed. Haves a look at the session.gc_maxlifetime, session.gc_probability, session.gc_divisor directives.

    For examples when:

    session.gc_divisor = 10
    session.gc_maxlifetime = 300
    session.gc_probability = 1

    Sessions will be destroyed when active fo 5 mins (200 seconds) and the garbage collection (destruction) of these sessions will occur in 1 out of every 10 requests.
    I already Set the Session Maxlifetime to 0
    what do you mean by XMLHTTPRequest object ?

    Quote Originally Posted by penagate
    By default, the session cookie is destroyed when the browser window is closed, which effectively logs out the user.
    Nop i can still see it on tmp folder

    Quote Originally Posted by JRSofty
    You can unset the $_SESSION variable but that only removes all the elements of the array
    Can i unset the $_SESSION variable on the onunload of the page ?



    Thanks !

  8. #8
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149

    Re: Destroy Session When Closing The Current Window

    Only if you are able to send a request using JavaScript to a PHP file that basically has
    Code:
    <?php session_start();
    unset($_SESSION);
    ?>
    To do that from the browser when they close the browser you would need to send the request via the XMLHTTPRequest object of JavaScript.

    The other option would be to try to use JavaScript to delete the session cookie left in the browser. Normally the cookie is called PHPSESSID or something along those lines (you can find out what the name is via the phpinfo() function).

    In all these cases however, could cause problems for your visitors when they jump from page to page on your site unless your site has only one page then the question would be why do you need to have sessions.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Destroy Session When Closing The Current Window

    Quote Originally Posted by JRSofty

    To do that from the browser when they close the browser you would need to send the request via the XMLHTTPRequest object of JavaScript.
    How via the XMLHTTPRequest object of JavaScript. ?


    Thanks !

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Destroy Session When Closing The Current Window

    You don't need to do that. PG has already pointed out that when the browser window is closed the cookie will be deleted and/or no longer used. If it is not ensure that the session.cookie_lifetime directive is set to zero.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149

    Re: Destroy Session When Closing The Current Window

    VisualAd is right. If the cookie is deleted the session is no longer valid. The session file will still be there until the garbage collection cleans it up but that is not a problem. Without a session cookie the next time they visit they will start a new session.

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Destroy Session When Closing The Current Window

    Use session_destroy() rather than unset($_SESSION) to invalidate the cookie and session data.


    (The manual will have you believe that session_destroy doesn't delete the cookie. In my experience it does.)

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Destroy Session When Closing The Current Window

    Yes , I use only session_destroy() ,
    but i need the garbage collection to clean the file in a 0 (s)
    i made session.gc_maxlifetime = 0
    so when i go by logout the file is deleted at the sametime
    but when i close the window it still on the folder
    as you said penagate

    By default, the session cookie is destroyed when the browser window is closed, which effectively logs out the user

    what i need is the file for this session to be deleted like with logout

    Thanks !

  14. #14
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Destroy Session When Closing The Current Window

    Why do you need that? Are you storing sensitive data in session variables? (This is a bad idea, for this reason.)

    It shouldn't matter exactly when the session data files are deleted. The fact that it does suggests that your application design might be improved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width