There are a couple of ways of telling whether someone is still looking at a page on your site. They're not pretty. Here's one:
Serve up framed pages. Arrange to "hide" a frame. In that hidden frame, set up a "watchdog timer" using a page with a meta refresh tag to a CGI on your site. The CGI keeps track of who is still out there polling, and returns the same page. After some suitable interval of not getting a poll, mark the user as logged out. You'll also need to use a cookie to distinguish people who may be sitting behind a firewall that's doing NAT (network address translation).
This works. People will throw bricks at you, but it does work.