Because PHP is a server side language, it's done processing as soon as the requested page is generated. It doesn't keep running on the client machine to check whether they're still active on the site.
If a user requests a page and promptly closes their browser, the server (read: PHP) is none the wiser.
The only way to track users on the server side, then, is to set a time threshold; for example: 15 minutes. Any user that requests a page is considered "logged in" for the next 15 minutes. If they don't request a page within 15 minutes of their previous request, they are considered "logged out".
If you're storing user activity in a database, you can simply select ALL the users who's last-active timestamp is less than 15 minutes from the current time.
Take care,
Nik
http://www.bigaction.org/