Hi

I want to destroy the session variables when the user closes the browser window. I tried the following code (found from the net) but it doesn't seem to work. It is not destroying the session variables and session_unset.php is being called even when I click on someother link (to load another page) on index.php.
What could be the problem?

index.php

<script src="redir.js" type="text/javascript"></script>
<body onUnload="redir();">

redir.js

function redir()
{alert("getting in");
window.location="http://localhost/session_unset.php";
//alert("trying to unset");
}

session_unset.php

<?php
ob_start();
@session_start(); 
header("Cache-Control: public, no-cache");
@session_unset();
@session_destroy();
@$_SESSION=array();
?>

any help is much appreciated

    Considering that per-session cookies (e.g. cookies with a '0' expire/time-to-live value) are deleted by the browser when it's closed, why are you even trying to do this at all?

    varai wrote:

    session_unset.php is being called even when I click on someother link (to load another page) on index.php.

    Well of course - that's what you've told the browser to do every time it unloads the current page.

      Hi,

      Yes, I also read that session cookies get deleted when the browser window is closed.

      Today, when I tested it again, I realized, that the session cookies get deleted only when I close the entire browser. However, when I close a single tab, they remain the same.

      Is it possible to destroy the session cookies when I close a single tab?

      The same thing happens in Firefox as well.

      By the way, I'm using IE8 and Mozilla Firefox 3.6.13.

      thank you.

        I don't see how it would be possible, no. That's probably why you see banking sites and whatnot tell you to completely close your browser window.

          ok, thanks a lot bradgrafelman. 🙂

            Write a Reply...