hi all

i have my lougout.php which destroys sessions and session ids on logout. 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 then all session and session ids are destroyed automatically but these sessions are not destroying when the user closes the "browser tab".

Its not possible with javascript but its possible with AJAX. I have not used ajax uptil now and have no knowledge. So it would be great if somebody helps me with calling my logout.php script on
closing "browser tab".

this is my logout 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"); 
?>

vineet

There is no reliable/standard way of detecting a browser close event. Though there are non-supported workarounds, a browser close event is not something you should rely on.

Anyways, here is a snippet which seems to be working fine on IE. If you are new to Ajax, I recommend using the AjaxToolbox library to do away with writing most of the boilerplate code.

<!--
    Make async request when a browser window is closed.

    Copyright (C) 2008  sos aka Sanjay

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Script-Content-Type" content="text/javascript">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Example</title>
    <script type="text/javascript" src="AjaxRequest.js"></script>
    <script type="text/javascript">
    // Error checking omitted for brevity
    function doIt() {
      var o = {
        "url": "http://your.domain.net/logout",
        "onSuccess": function(req) {
          alert("status: " + req.status + "\nStatus text: " + req.statusText);
        },
        "onError": function(req) {
          alert('Error!\nStatusText='+req.statusText+'\nContents=' +
              req.responseText);
        }
      };
      AjaxRequest.get(o);
      alert("Request sent; check server logs....");
    }
    window.onunload = doIt;
    </script>
</head>
<body id="bdy">
  <p>Close this window to test forced browser expiry.</p>
</body>
</html>
This article has been dead for over six months. Start a new discussion instead.