0

Hi,

How can we destroy session after closed window or tab? If user close window, still session is alive after login the systems. Please give me good solutions.

thanks for your time.

William

9
Contributors
14
Replies
46
Views
5 Years
Discussion Span
Last Post by jkon
0

try the following in the function which closes the window or tab

unset($session_variable)

you can also try
destroy, session_destroy() methods

0

tq for reply. But its not working when you close tab or window.I already try. Do you have any sample code for that..

0

He's not looking for PHP code, but for a Javascript event to trigger the PHP session destroy (I think).

See this, it's an example with jQuery. In that event, you can use AJAX to call a PHP function to clear the session.

Edited by pritaeas: n/a

0

Hi , i need sample code with jquery and call php file to destroy session when user close the tab or window or refreshing page. Whenever i click the menus in the page, no need to do anything.

when user close the window or tab or refreshing page, then call the php function.

thank you for your time..

william

0
<script language="javascript">
<!--
window.onbeforeunload = domystuff;

function domystuff(){
   //...
}
//-->
</script>

Just run a regular ajax script from domystuff().

0

i tried using window.onbeforeunload. But its not calling the fucntion. any possible?

1

hai,

i give the code now. this code working only when user close the window. Its not working when user close the tab. see the code below.

<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","userlogout.php",true);
  xmlHttp.send(null);
  }
</script>

then i call the function in body

<body onunload="ajaxFunction()">

content

</body>

any help to destroy session when user close the tab?

william

Edited by Dani: Formatting fixed

0

Is it an idea to auto expire a session after x minutes? So for example when a user leaves your website and remains inactive for 10 minutes, after which he returns, your system detects he has been away too long and unsets the session?

0

my question is :i want to close browser show alert but alert doesnot show during refresh a page like f5 and url select thn press enter.in jquery have any soluatin rely mme fast ...................thnks

0

SHow your code - we don't have a crystal ball. Also 'fast' is as fast as we decide it is. We are volunteers not slaves. Unless you want to pay for a premium service?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.