Advanced Search

Results 1 to 3 of 3

Thread: AJAX- Destroying session on closing browser tab or body unload

  1. #1
    Join Date
    Jun 2008
    Posts
    114
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default AJAX- Destroying session on closing browser tab or body unload

    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".


    This is ajax script
    Code:
    <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

    Code:
    <? 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
    Code:
    <body onunload="ajaxFunction()">
    vineet

  2. #2
    Join Date
    Oct 2008
    Posts
    40
    Thanks
    3
    Thanked 1 Time in 1 Post

    Smile

    May be this function will help you ................
    But,
    A browser tab is a window. There is no specific way to determine if it is a tab or an actual window that you are closing (using javascript).

    Code:
    <script language="JavaScript">
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
      }
    </script>

  3. #3
    Join Date
    Jun 2008
    Posts
    114
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default AJAX-destroying session

    Quote Originally Posted by pankaj.ghadge View Post
    May be this function will help you ................
    But,
    A browser tab is a window. There is no specific way to determine if it is a tab or an actual window that you are closing (using javascript).

    Code:
    <script language="JavaScript">
      window.onbeforeunload = confirmExit;
      function confirmExit()
      {
        return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
      }
    </script>
    First thing is that The your code provided is asking for the confirmation everytime i refresh the page. confirmation should come only on body unload.

    Second thing i would like to ask that i have
    function ajaxFunction().
    I want to call this function means this function should work only when the person presses ok on our confirm alert and if he presses cancel then this function should not work. also i dont want to call this ajax function on body unload, its causing problem. it should be called only if the person presses ok.

    how can i call this function on ok. if you tell me this it would be great help.

    vineet

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •