You cannot do that.
- You cannot rely on a event happening on the browser
- There is no way the server can contact the browser to see if it is alive
You do not control the browser of the end-user. The user might close the browser, crash, disconnect from the Internet, have javascript disabled or crashed, etc, etc. Or the user might just enter another url (and after that the user might return to your page by using the back button, and expect to continue his/her session with your site).
The window.onbeforeunload is triggered with every unload of the page. Even if the user browses to another page on your site, so they might need your session again.