Hi.
This is a common problem, without a very good solution.
The problem here is that there is no reliable way of tracking whether the user has actually closed the browser or not.
The best solution I have seen is a timeout based logout system.
That is, whenever the client opens a page, log the time into a database.
If the client remains inactive, not requesting new pages, for a given amount of time, he will be considered offline.
You could even expand this to include AJAX, which would periodically update the database time-stamp while the page is open, thus keeping the user online if for some reason he spends more time than the timeout period on a single page.