0

I want to destroy session when i close window so i am using a code

java script code:

<script>
function killSession() {
    location = 'index.php?destroySession=true';
}
 </script>

php code:

<?php 
     if (isset($_GET['destroySession']) && $_GET['destroySession'] == "true") {
    session_destroy();
    $closeWin = "window.close()";
} else {
    $closeWin = "";
}
?>

body:

<body onload="<?php echo $closeWin; ?>"  onunload="killSession();">

But its not giving me seficient response.I want to destroy session when window close.How its possible ?

Mak
  • 191
  • 1
  • 2
  • 11
  • 1
    I can't imagine a world in which browsers would allow this. – Ja͢ck Oct 22 '12 at 05:42
  • Possible duplicate: http://stackoverflow.com/questions/2839988/destroy-or-unset-session-when-user-close-the-browser-without-clicking-on-logout – Jan Oct 22 '12 at 05:46
  • possible duplicate of [How do I expire a PHP session after 30 minutes?](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) – Brian Diggs Oct 22 '12 at 15:48

3 Answers3

1

PHP Sessions get expired when the browser closes.. http://www.php.net/manual/en/function.setcookie.php see the manual now if this not happening there should be some issue with modified configuration files.

Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45
0
 $closeWin = "window.close()";
 echo "<script>".$closeWin."</script>";
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
0

Remove onload="<?php echo $closeWin; ?>" from your code first and made change into your php code as below:

<?php 
     if (isset($_GET['destroySession']) && $_GET['destroySession'] == "true") {
        session_destroy();
        $closeWin = "window.close()";       
        echo "<script>".$closeWin."</script>";
}
?>

Hope this will solve your problem

pkachhia
  • 1,876
  • 1
  • 19
  • 30
  • i have tried this code but when we start session that time is destroy session. – Mak Oct 22 '12 at 06:45
  • i mean when i login as user,session starts and automatically logout.i want to destroy session when i forgot to logging out.I think this is not working well is there any other way ? I am finding on google from last 2 days but i didn't get saficient answer.If you did this type of work before so kindly help me. thanks – Mak Oct 22 '12 at 07:02