Here is my problem. I have a site into which users can login. It has a logout button to let the user log out. My problem is when they close the browser without logging out, how can I close the logged in entry in the database?? I need a way to run a piece of php script when the browser closes or if a session ending can trigger this. (Since the session is suppose to end when they close the browser.)

7 Years
Discussion Span
Last Post by diafol

The session isn't supposed to end when the browser closes. You can run a js script, which could do it - through ajax (php).

$(document).ready(function() {

 $("a").click(function() {
   remain = this.href.Search("www.example.com"); 
   if (remain == -1) {
     return(confirm("You wanna logout first?"));
   return true; 


I got this snippet a while ago from somewhere, never used it though. Personally, I think it's rude to popup on exit. A far better solution would be to run a cron job to delete obsolete db sessions or use 'garbage collection'.

Edited by diafol: n/a


i do not want to pop up anything on logout. And i dunno how to run a cron job. Plus i hav hosted on a free hosting account. Any other alternative??


Use 'garbage collection' method. Google it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.