简体   繁体   中英

How To Stop Destroy Session

When i close windows tab, session was dead! how can i stop that ?

i use this :

session_start(['cookie_lifetime' => 86400,]);

but when user close tab or move to another page session was dead !

example :
i'm in page => "home"
when i try to go this url "example.com/users"
the session was dead.
• please note this , this problem is just in my website, i can use that ( users page ) in "localhost". but i never can't close browser ( in both (localhost/website) ,
i guess if session will alive for long time ( example 1 day ) , the problem could solved.

thanks.

a session stays active as long as the browser is active, when it closes the session closes as well. If you want to stop this from happening i recommend you to create a cookie instead. Read all about it here

cookie example from W3schools on how to create a cookie:

<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");   //    86400 = 1 day
?>
<html>
<body>

<?php
  if(!isset($_COOKIE[$cookie_name])) {
     echo "Cookie named '" . $cookie_name . "' is not set!";
  } else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
 }
?>

</body>
</html> 

AND the way you are trying to use a session is wrong!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM