You'll need to send an AJAX request to the server using the onunload event of the body. Look on google to find a good tutorial on AJAX like this or this or this...
That probably wouldn't work if the browser crashes, but I'm not sure. You could also send a periodic request from every page (like once a minute or something) and if that "keepalive" request doesn't come in the session is invalidated.