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

Roshan_10 commented: Set session time out for every 24hr. And now check if session exists or not. If it doesn't exist, execute query to insert necessary log into database. +0
Member Avatar for 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'.

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

commented: Set session time out for every 24hr. And now check if session exists or not. If it doesn't exist, execute query to insert necessary log into database. +0
Member Avatar for diafol

Use 'garbage collection' method. Google it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.