How can I handle when SESSION END (session_destroy()) in PHP raise!!!

When the user close the browser - SESSION of that user die, how can I handle on server (with my PHP server logic) this event...

** In MS ASP.NET I can handle that event in Session_End procedure.....**

I need when the user close the browser handle the end of his session on server and
update info about this user in database MySQL with such SQL-query:

UPDATE Users
set Active=0
where id=some_id

Thank's for help....from sema....

    You might find that you can catch a browser unload event (not quite a close event) with JavaScript and get it to load a PHP script to tidy up:

    <script language="JavaScript">
    function unLoad() {
      window.open("session_end.php");
    }
    </script>
    
    <body onUnload='unLoad()'>
    ...
    

    The put your tidying up code in session_end.php. I haven't tested this, but you'll probably need to return some HTML from session_end.php to say "Please close me".

      The manual chapter on Connection Handling might be useful to you (http://www.php.net/manual/en/features.connection-handling.php).

      Or you could let the session time out normally (especially if you have the session data stored in the database; a trigger that fires when a session table entry is deleted could run to update the online user table).

      Or (and it's fun to come up with at least three solutions), try out PHP's dotnet extension and see if you can use ASP.NET's Session_End.

      Whichever; as I understand it, ASP.NET's Session_End works off a timer the same way that PHP's session-handling does. So what you're after is a way to tell if a user still has a current session. The easiest way to do that is still to have the session handled in the database; using file-based sessions means examining through all the current session files and seeing which users are represented there.

        Write a Reply...