I'm not sure if you can do this with PHP, but here's what you 'could' do if it were java - perhaps PHP has something similar to this:
when a user logs in, add their information to a list stored globally (in java this could be in a singleton. with .net you can simply store this in the Server object). every time someone hits the server with a link or button click, it resets a time stamp. run a thread that checks for 'dead users' every couple of minute - if the time stamp is stale (has exceeded the max time allotted for a live session -eg: past 20 minutes) stamp them as 'logged out' in the database. if they were logged in and created a session, that session would have expired at 20 minutes (default setting) anyways and they'd have had to relogin.
My product, also written in php is similar. What you need to do is, rather than just stamp a time for login/logout, is create a session object on server side. I have a simple process, written in c, that monitors those sessions and does numerous things. One of its functions is to end the session when the timeout value has been reached.