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

Recommended Answers

All 14 Replies

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

unset($session_variable)

you can also try
destroy, session_destroy() methods

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

please share your code

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.

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

Member Avatar for diafol
<script language="javascript">
<!--
window.onbeforeunload = domystuff;

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

Just run a regular ajax script from domystuff().

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

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

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?

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

Member Avatar for diafol

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?

@swilliamrobert Thanks for your sharing. Your code working awesome :)

thak u sooo much bro .its working .............................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.