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.
qdcsincAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

virmaiorCommented:
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 :

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

setTimeout('reloadQ();',1000*60*5);
function reloadQ()
{
  location="/hidden_frame/stillhere.php";
}
</SCRIPT>
</HEAD>
<BODY>
&nbsp;
</BODY>

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
qdcsincAuthor Commented:
Thanks,

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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.