We help IT Professionals succeed at work.

Automatically logging off users when browser is closed

warlords_inc asked
Last Modified: 2006-11-17
I've got a login process, that once a user logs into the website, the ip address is recorded.  So when the browser is closed, and re-opened, it checks against the session table, finds the ip address, and keeps the user logged in.  If the ip address is not found, the user must log in again.  When the user logs out, it deletes the user's ip address.

I am really trying to find all members who are logged into the website.  If I keep them logged in even when they close the browser, then the online count will be inaccurate.  But I don't want to log them out of the game, if they are just sitting in the game not doing anything.

So once they log in, they stay logged in until they either close the browser, or log off.

Logging off deletes the ip address from the sessions table.
Closing the browser does ........................

Does anyone have any ideas how I can go about doing this?

Watch Question


I might be going about this process the wrong way too, because as the user goes from page to page, there will be a check to make sure that they are the correct person as they make changes.  I can record each check and update a time field for last action taken(which I will keep track of anyway to check for dead accounts.)

But I don't want them logged out due to time, because someone might just be sitting on a page not doing anything, but I still want to count them as logged in.

Any and all suggestions will be entertained.

Try with an onunload in the body tag to run a javascript function so that when the user closes a browser window a popup window is opened which runs a php page which uploads the logout information as you have stated above.  Once this php page has closed then you can have javascript close the popup window automatically.  

You will probably have to look into some way of determining if the unload event is caused by the user clicking on the "X" or close option or if the unload is triggered by the user clicking on a link on the page as both results will have to have different outcomes i.e. 1 closes brower and records logout, one keeps user logged in and loads next page (again with the unload event in the body).

Hope this is of some use.


That would work exactly as I'm wanting it too, except....
when you move to another page, the window pops up.  And I don't see a way to set it to only unload when exiting the browser.  

What if you passed a variable from page to page within a link, then reset the variable after the body tag, so that if you then exited the page, the variable wouldn't set, and the new window would pop up.

Or another way....
What if you set up a small frame in the page, that had the onunload tag.  Then keep the frame open the entire session, just use the main one.  Then when the user exits the browser the window pops up, and the user is logged off.
I once saw a website with a 1 pixel sized frame, the same color as the background.  The writer used it for an applet game that had a music option, click a button and the frame would jump to the corresponing frame.  And if he didn't tell you how he did it, you would have swore that the applet was really playing the music.

I'll test it and see what I can come up with.


I set up a test page and so far it looks like this will work, only a few problems.

If you hit refresh, the frame also refreshes and pops up the window.  You could set it with a 'logout confirmation button', press yes to logout, press cancel to stay logged in.  But some may just try and close the window, which will not post anything(or add another pop up, by this time the user is getting mad).  Also if the user has a pop-up blocker will this block the window?

This is a close solution, however if it wasn't for the refresh problem, I would call it close enough.  I'm going to leave this open a little while longer to see if someone has a better idea, or a fix for the refresh problem.
This one is on us!
(Get your first solution completely free - no credit card required)
To check if a user is still online if he has idle browser page open, you need a javascript Timer (http://www.google.com/search?q=javascript+Timer), which would poll a server every minute or so. On the server half you just need to check for timeout in your ip table with every other user request.
Request to server can be done as dynamic image. For example:
<img src="picture.gif.php" name="myImageName">

<script language="JavaScript"><!--
function reloadImage() {
    var now = new Date();
    if (document.images) {
        document.images.myImageName.src = 'picture.gif.php?' + now.getTime();


Your script are required to return an image with correct Content-type: web-server header.


I thought about the java timer, I can update the last action time in the sessions table, everytime the picture gets refreshed.  But wouldn't that be a lot of calls to the sever if its refreshing every minute?

I'm not sure how to use the session variables, do you have an example page I can look at?  If you use this, and the browser is closed, then how does the server know that the session has ended?
Do not worry about server load. When you load a page - a request is made to server for every image page contains and if user every minute reloads browser - server load is a lot more heavy.

These variables are set up in your php.ini file and info about session I've got from manual page here:

Answering your last question, if server will not receive a request from a user for a specified amount of time (2 minutes or so) - it must delete ip address from table and close the session.

To open or check session specify
when check if a variable is registered
    if (array_key_exists('active', $_SESSION)) ... ;
to register variable to a session use
    $_SESSION['active'] = 1;

Experiment! =)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.