That is typically done by logging the time when a given user (or a guest, if you want that) was last seen.
Once a user hasn't been seen for some predetermined amount of time, he is considered off line.
This is really the only reliable method, given the stateless nature of the web.
This was actually the script I was going to suggest. You could implement it with your current system and possibly identify the IP addresses by the IP addresses your users logged in with. Just a thought.
I think you can done this idea by session (for members)
like if session user is available so the user is here
IP address is unreliable.
DB sessions could be a way to go, although last_impression timestamp for logged on users can be used as a cut-down version. You need a clean up with these though (php script or MySQL trigger), unless you run a cron job every 20 minutes or so.