hi all

i have my logout.php which destroys sessions and session ids on click of logout buton. I want to have same effect means i want to call this logout.php script when the user closes the "browser tab".

as these are the days of "browser tabs" so this is very important for me.

when the user closed the whole browser or logout then all session and session ids are destroyed automatically but these sessions are not destroying when the user closes the "browser tab".

I have tried ajax also but not able to get the result as i m not much in ajax. So it would be great if somebody helps me with calling my logout.php script on closing "browser tab".

any other method is also welcome

This is ajax script

<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.myForm.time.value=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET","customer_logout.php",true);
  xmlHttp.send(null);
  }
</script>

This is my customer_logout.php script

<? require_once("config.php");
$unique_id = session_id(); 
session_regenerate_id();
$qry="delete from cart_table where unique_id='$unique_id'";
mysql_query($qry);
$_SESSION = array(); 
session_unset();
session_destroy();
header("Location:index.php"); 
?>

This is i calling ajax in html

<body onunload="ajaxFunction()">

vineet

    Never done this before but this is how I'd do it too. My guess is that the browser gets closed before javascript can fire the async request. Have you tried using the onBeforeUnload event?

      bretticus;10893660 wrote:

      Never done this before but this is how I'd do it too. My guess is that the browser gets closed before javascript can fire the async request. Have you tried using the onBeforeUnload event?

      hi bretticus

      sorry i didnt noticed a new problem that occurs when using this ajax code wether with body onunload or bodyonbeforeunload that
      everytime i switch over to other page and left the page which has this ajax code the session gets expired.

      means if i have this code on index.php and i move to contact us page , at that moment the session wil get expire.

      can u suggest some solution.

      vineet

        Oh right, *unload doesn't care if you are closing or navigating away from the current page! 😃

        Wow, I have thought about this and cannot think of anyway of detecting that the tab is closing. Sorry!

          4 years later

          Hello ,

          Can anyone tell me how to destroy the session when the user closes the tab ?

          advance thanks.

            You can't. A lot of browsers are now preventing any requests when the tab/window is closed. This is for security reasons. The best you could do is to let the session time out naturally and the server will clean it up as necessary.

              Write a Reply...