What is the best way to check and see if a user has closed the browser and or disconnected from your game?

I am making a flash/php game and I am open to suggestions.

JS or PhP?

I see a lot of people asking about this but I dont find much info? Any one help me out or steer me in a direction to find out?!

Thanks!