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

Posted on 2006-04-24
Last Modified: 2013-12-12
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.
Question by:qdcsinc
    LVL 20

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

    Author Comment


    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now