PHP - 关闭浏览器后会话销毁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24402047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Session destroy after closing browser
提问by Shri
Though this question has multiple duplicates i could not find proper solution for me. Need Some help.
虽然这个问题有多个重复,但我找不到合适的解决方案。需要一些帮助。
I have used ini_set('session.cookie_lifetime', 0);
in my configuration file.
我ini_set('session.cookie_lifetime', 0);
在我的配置文件中使用过。
But it is not helping me to destroy session on browser close.
但这并不能帮助我在浏览器关闭时销毁会话。
Application current flow:
应用电流:
1) In authentication page if user is valid, generate new session identifier using session_regenerate_id(true);
1) 在身份验证页面中,如果用户有效,则使用生成新的会话标识符 session_regenerate_id(true);
2) Control goes to welcome.php where i start new session using session_start();
2)控制转到welcome.php,在那里我使用 session_start();
3) in logout page code is
3)在注销页面代码是
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
采纳答案by Ankit
The best way is to close the session is: if there is no response for that session after particular interval of time. then close. Please see this post and I hope it will resolve the issue. "How to change the session timeout in PHP?"
关闭会话的最佳方法是:如果在特定时间间隔后该会话没有响应。然后关闭。请看这篇文章,我希望它能解决这个问题。“如何在 PHP 中更改会话超时?”
回答by colburton
Use a keep alive.
使用保活。
On login:
登录时:
session_start();
$_SESSION['last_action'] = time();
An ajax call every few (eg 20) seconds:
每隔几秒(例如 20 秒)调用一次 ajax:
windows.setInterval(keepAliveCall, 20000);
Server side keepalive.php:
服务器端keepalive.php:
session_start();
$_SESSION['last_action'] = time();
On every other action:
在所有其他动作上:
session_start();
if ($_SESSION['last_action'] < time() - 30 /* be a little tolerant here */) {
// destroy the session and quit
}
回答by Adil Abbasi
This might help you,
这可能对你有帮助
session_set_cookie_params(0);
session_start();
Your session cookie will be destroyed... so your session will be good until the browser is open. please view http://www.php.net//manual/en/function.session-set-cookie-params.phpthis may help you.
您的会话 cookie 将被销毁...因此您的会话将一直有效,直到浏览器打开。请查看http://www.php.net//manual/en/function.session-set-cookie-params.php这可能对您有所帮助。
回答by kpp
There are different ways to do this, but the server can't detect when de browser gets closed so destroying it then is hard.
有不同的方法可以做到这一点,但是服务器无法检测到浏览器何时关闭,因此很难破坏它。
- timeout session.
- 超时会话。
Either create a new session with the current time or add a time variable to the current session. and then check it when you start up or perform an action to see if the session has to be removed.
使用当前时间创建新会话或向当前会话添加时间变量。然后在启动或执行操作时检查它以查看是否必须删除会话。
session_start();
$_SESSION["timeout"] = time();
//if 100 seconds have passed since creating session delete it.
if(time() - $_SESSION["timeout"] > 100){
unset($_SESSION["timeout"];
}
- ajax
- 阿贾克斯
Make javascript perform an ajax call that will delete the session, with onbeforeunload()
a javascript function that calls a final action when the user leaves the page. For some reason this doesnt always work though.
使 javascript 执行一个将删除会话的 ajax 调用,并使用onbeforeunload()
一个 javascript 函数在用户离开页面时调用最终操作。出于某种原因,这并不总是有效。
- delete it on startup.
- 在启动时删除它。
If you always want the user to see the login page on startup after the page has been closed you can just delete the session on startup.
如果您总是希望用户在页面关闭后在启动时看到登录页面,您可以在启动时删除会话。
<? php
session_start();
unset($_SESSION["session"]);
and there probably are some more.
可能还有更多。
回答by itzmukeshy7
You can do it using JavaScript by triggering an ajax request to server to destroy the session on onbeforeunload event fired when we closes the browse tab or window or browser.
当我们关闭浏览选项卡或窗口或浏览器时,您可以使用 JavaScript 通过触发对服务器的 ajax 请求来销毁 onbeforeunload 事件上的会话。
回答by DTT
There's one more "hack" by using HTTP Referer (we asume that browser window was closed current referer's domain name and curent page's domain name do not match):
使用HTTP Referer还有一个“hack”(我们假设浏览器窗口被关闭,当前referer的域名和当前页面的域名不匹配):
session_start();
$_SESSION['somevariable'] = 'somevalue';
if(parse_url($_SERVER["HTTP_REFERER"], PHP_URL_HOST) != $_SERVER["SERVER_NAME"]){
session_destroy();
}
This also has some drawbacks, but it helped me few times.
这也有一些缺点,但它帮助了我几次。
回答by Braike dp
If you want to change the session id on each log in, make sure to use session_regenerate_id(true) during the log in process.
如果您想在每次登录时更改会话 ID,请确保在登录过程中使用 session_regenerate_id(true)。
<?php
session_start();
session_regenerate_id(true);
?>
回答by user3774008
Use the following code to destroy the session:
使用以下代码销毁会话:
<?php
session_start();
unset($_SESSION['sessionvariable']);
header("Location:index.php");
?>
回答by Mr.Unknown
If you are confused what to do, just refer to the manual of session_destroy()function:
如果您不知道该怎么做,请参阅session_destroy()函数的手册:
http://php.net/manual/en/function.session-destroy.php
http://php.net/manual/en/function.session-destroy.php
There you can find some more features of session_destroy().
在那里您可以找到 session_destroy() 的更多功能。