Detect browser close

I’m looking for some way to perform some action when the user closes the web-page in which the SWF is playing (basically, I need to log a player out of the system if he closes the page or tab). I read that it should be possible using ExternalInterface, but I couldn’t make it work. I’d appreciate it if someone could just show me a comprehensible JS and AS3 code that works, or tell me if there’s some other solution.