You can always tell when someone has hit your page... but droping off their connection is something quite different. You cannot in real time detect that someone has left your site. You could install a "timeout" though. Say your user wants X.php so as soon as he gets it PHP collects the Username and timestamp and loads em into a database. Now when you want to right your script to see who is online, you say, ok who hasnt been active for say 5 minutes, these are the ppl that have "exited" you site. Now is this perfect? No absolutly not. You might get ppl who are just sitting there with there browser open or something.
The problem with your suggestion is this. Closing the browser is a User action. There is no way for the server to detect the browser has been closed.
Hope this Helps!