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.)

Member Avatar

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(""); 
   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'.

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??

Member Avatar

Use 'garbage collection' method. Google it.