I need to clear the session variables when the tab is closed but I could not find any solutions so far. The closest I have got is by using the "onbeforeunload" function is javascript.
<body onbeforeunload='destroySession()'>
<!--Codes for the site includeing php scripts-->
</body>
<script type='text/javascript'>
function destroySession(){
$.ajax({
url: "destroySession.php"
});
}
<script>
The problem is the function is called every time a new link is clicked, refreshed or even if a form is submitted. Is there a better way to destroy session variables on closing a tab or am I doing something wrong? Please help.