possibly javascript. php is server side, and has no idea what is happening on the user's end unless it is sent something. i don't do javascript though, so someone else is going to have to chime in.
the only way to do it purely in php would be to save a timestamp when someone logs in, and if their session has been inactive for X number of minutes then flag them as logged out. this script would likely be set up to run every X minutes with cron, and will likely hit your database more times than it is worth. it won't have anything to do with the window closing though. if you're interested in this method, a helpful tutorial can be found over at codewalkers.com:
http://codewalkers.com/tutorials/14/1.html