• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Update MySql Database Via PHP After Window is Closed for a Logout

I have a MySql Database Table that notes the date, time and userid of any person logging in and the date time and userid of them when they click the "LOG OUT" button.

However, if they close the window, they are not logged out.

I've tried Javascripts "onUnload" event handler but this is no good for two reasons.

1) Most popup blockers (including my own) won't let another script pop-up

2) The "onUnload" event handler is called not only when a window is closed but whenver the browser url changes.

In searching experts-exchange, a suggestion I saw was to combine php with Cron-Tabs.

I know how to setup a Cron-Job via my Cpanel Hosting Account.

Is there a way to control Cron-Tabs within PHP?

Even if I could, I understand the Cron-Jobs simply work on a schedule (every 15 minutes or every 15 days).  How could a Cron-Job respond to a browser window being closed.

I'm due to turn in this project (along with my final invoice) very soon.
1 Solution
the trick to using a cron-tab approach lies in having set timer code in your page...
so rather than figuring out when they close the window, figure out when the window stops telling you that it's open.

assumption (storing user_id as $_SESSION variable...)

stillhere.php :

mysql_query('UPDATE still_here SET last_here = NOW() WHERE user = ' . $_SESSION['user_id']);
<TITLE>Still Here</TITLE>

function reloadQ()

then with your cron job, check to see which entries in the DB are older than 5 minutes.  these users are no longer logged in.

make sense?
qdcsincAuthor Commented:

I can see how this will work, though I didn't use a SESSION variable.  ( I now wish that I had).

This is very informative

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now