为什么 PHP session_destroy() 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6472123/
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
Why is PHP session_destroy() not working?
提问by Rajasekar Gunasekaran
I tried to destroy all session variable by using the session_destroy()
method, but after using this method, the values are not destroyed.
我尝试使用该session_destroy()
方法销毁所有会话变量,但使用该方法后,值并未销毁。
Why is session_destroy()
not working?
为什么session_destroy()
不工作?
Is there any other way to destroy the session in PHP?
有没有其他方法可以破坏PHP中的会话?
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800))
{
session_destroy();
session_unset();
}
回答by R.D.
Perhaps is way too late to respond but, make sure your session is initialized before destroying it.
也许来不及响应,但请确保您的会话在销毁之前已初始化。
session_start() ;
session_destroy() ;
i.e. you cannot destroy a session in logout.php if you initialized your session in index.php. You must start the session in logout.php before destroying it.
也就是说,如果您在 index.php 中初始化会话,则不能在 logout.php 中销毁会话。在销毁它之前,您必须在 logout.php 中启动会话。
回答by Andrea
After using session_destroy()
, the session is destroyed behind the scenes. For some reason this doesn't affect the values in $_SESSION
, which was already populated for this request, but it will be empty in future requests.
使用后session_destroy()
,会话在幕后被销毁。出于某种原因,这不会影响 中的值$_SESSION
,该值已经为此请求填充,但在未来的请求中它将为空。
You can manually clear $_SESSION
if you so desire ($_SESSION = [];
).
$_SESSION
如果您愿意,您可以手动清除( $_SESSION = [];
)。
回答by kevinji
If you need to clear the values of $_SESSION
, set the array equal to an empty array:
如果需要清除 的值$_SESSION
,请将数组设置为空数组:
$_SESSION = array();
Of course, you can't access the values of $_SESSION
on another page once you call session_destroy
, so it doesn't matter that much.
当然,$_SESSION
一旦调用session_destroy
,就无法访问另一个页面上的 值,因此无所谓。
Try the following:
请尝试以下操作:
session_destroy();
$_SESSION = array(); // Clears the $_SESSION variable
回答by yitwail
Actually, it works, but you also need to do $_SESSION = array();
after the session_destroy to get rid of $_SESSION variables. However, avoid doing unset($_SESSION)
because that makes sessions useless.
实际上,它有效,但您还需要$_SESSION = array();
在 session_destroy 之后执行以摆脱 $_SESSION 变量。但是,避免这样做,unset($_SESSION)
因为这会使会话无用。
回答by Dave Henderson
Well, this seems a new problem for me, using a new php server. In the past never had an issue with sessions not ending.
嗯,这对我来说似乎是一个新问题,使用新的 php 服务器。过去从未遇到过会话未结束的问题。
In a test of sessions, I setup a session, ran a session count++ and closed the session. Reloaded the page and to my surprise the variable remained.
在会话测试中,我设置了一个会话,运行了一个会话计数++并关闭了该会话。重新加载页面,令我惊讶的是变量仍然存在。
I tried the following suggestion posted by mc10
我尝试了 mc10 发布的以下建议
session_destroy();
$_SESSION = array(); // Clears the $_SESSION variable
However, that did not work. I did not think it could work as the session was not active after destroying it, so I reversed it.
然而,这并没有奏效。我不认为它可以工作,因为会话在销毁它后不活动,所以我将其撤消。
$_SESSION = array();
session_destroy();
That worked, reloading the page starting sessios and reviewing the set variables all showed them empty/not-set.
那行得通,重新加载页面开始 sessios 并查看设置的变量都显示它们为空/未设置。
Really not sure why session_destroy() does not work on this PHP Version 5.3.14 server.
真的不知道为什么 session_destroy() 在这个 PHP 版本 5.3.14 服务器上不起作用。
Don't really care as long as I know how to clear the sessions.
只要我知道如何清除会话,就不必在乎。
回答by Mert S. Kaplan
session_destroy()
is effective after the page load is complete. So in the second upload, the session is terminated. But with unset()
you can also log out from within the page.
session_destroy()
页面加载完成后生效。所以在第二次上传时,会话终止。但是unset()
您也可以从页面内注销。
回答by Alisso
I had to also remove session cookies like this:
我还必须像这样删除会话 cookie:
session_start();
$_SESSION = array();
// If it's desired to kill the session, also
// delete the session cookie.
// Note: This will destroy the session, and
// not just the session data!
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();
Source: geeksforgeeks.org
资料来源:geeksforgeeks.org
回答by ashkan nasirzadeh
if you destroy the session on 127.0.0.1
it will not affect on localhost
and vice versa
如果您销毁会话,127.0.0.1
则不会影响,localhost
反之亦然
回答by Shankar Damodaran
It works , but sometimes it doesn't(check the below example)
它有效,但有时无效(请查看下面的示例)
<?php
session_start();
$_SESSION['name']="shankar";
if(isset($_SESSION['name']))
{
echo $_SESSION['name']; // Outputs shankar
}
session_destroy();
echo $_SESSION['name']; // Still outputs shankar
Weird!! Right ?
奇怪的!!对 ?
How to overcome this ?
如何克服这一点?
In the above scenario , if you replace session_destroy();
with unset($_SESSION['name']);
it works as expected.
在上面的场景中,如果你session_destroy();
用unset($_SESSION['name']);
它替换它会按预期工作。
But i want to destroy all variables not just a single one !
但我想销毁所有变量而不仅仅是一个变量!
Yeah there is a fix for this too. Just unset the $_SESSION
array. [Credits Ivo Pereira]
是的,这也有解决办法。只需取消设置$_SESSION
数组。[致谢伊沃佩雷拉]
unset($_SESSION);
回答by user3764953
Add session_start(); before !Doctype Declaration
添加 session_start(); 在 !Doctype 声明之前
<?php session_start(); ?>
<!doctype html>
<html>
<body>
<?php
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800))
{
session_destroy();
session_unset();
}
?>
</body>
</html>