PHP会话没有注销/未设置
我知道这个问题有很多重复,但我尝试了其中几个,没有一个已经被回答。PHP会话没有注销/未设置
这里是我的logout.php代码:
<?php
session_start();
require './codefiles/dbhelper.php';
$dbh = new DbHelper();
$dbh->Execute('UPDATE surveyors SET LoggedIn=\'0\', SessionID=\'\' WHERE Username=\''.$_SESSION['username'].'\'');
session_unset();
session_abort();
session_destroy();
$_SESSION = array();
unset($_SESSION['username']);
unset($dbh);
header('location:index.php');
?>
但会话变量太“执着”被删除。会话值不会被清除,会话变量也不会被删除。对象$ dbh未设置,但未设置$ _SESSION ['username'];
另一个无关的问题,尽管我设置LoggedIn = 0
,在我的SQL查询中,它只是在数据库中保持为1。 LoggedIn
字段的类型是'位'。 SessionID
字段设置为空白。
有什么解决方法吗?
编辑:
移除echo $dbh->error
,因为它是不必要的。
编辑2:
新增session_destroy()
通过霍山Magdy建议。
我不知道为什么,但破坏会话的代码在某种程度上不起作用logout.php。它工作在index.php等文件中,但会出现各种不可预知的行为。
找到了解决此问题的解决方法。该logout.php有如下代码:
<?php
session_start();
$_SESSION['logout'] = TRUE;
header('location:index.php');
?>
而这种代码添加到的index.php:
# Implement logout functionality
<?php
session_start();
if(isset($_SESSION['logout']) && $_SESSION['logout'] == TRUE){
foreach($_SESSION as $var => $value){
unset($_SESSION[$var]);
}
session_destroy();
session_unset();
}
?>
它可能不是一个标准化的解决方案,但这些代码对我的作品每一次,没有不可预知的行为。
感谢大家分享他们的想法。
我想foreach未设置必须在logout.php或login.php之前设置新的会话值,在index.php不工作,因为我摧毁了我需要的会议。无论如何,这是一个奇怪的问题。你测试了这个http://stackoverflow.com/questions/16759719/i-just-cant-destroy-a-session-in-php? –
正如我所说,我的代码在任何文件中工作,但不是在我的** logout.php **中(不知道为什么)。如果它不适合你,那可能是因为我们的逻辑不同。任何方式,您建议解决我的另一个问题的链接,每次注销后重新生成一个新的会话ID。谢谢。 –
<?php
include 'codefiles/dbhelper.php';
if(!isset($_SESSION['id']))
{
header ("Location: login_form.php");
}
else
{
session_destroy();
die('You have been logged out.<meta http-equiv="refresh" content="0;url=login_form.php">');
}
?>
这基本上是“注销”结构。
但'session_destroy()'只是不工作,既不在logout.php也不在任何其他页面。 –
试试这个
<?php
session_start();
require './codefiles/dbhelper.php';
$dbh = new DbHelper();
$dbh->Execute('UPDATE surveyors SET LoggedIn=\'0\', SessionID=\'\' WHERE Username=\''.$_SESSION['username'].'\'');
echo session_status() . '<br />';
session_unset();
session_destroy();
echo session_status();
// header('location:index.php');
让我们来看看session_status()说。 但我的项目未设置& &销毁工作。
尽管我使用session_start(),调用'session_destroy()'会抛出警告,如下所示:* Warning:session_destroy():尝试销毁C:\ xampp \ htdocs \ xxxxxxxxx \ index.php *中的未初始化会话。关于'echo session_status()',它打印** 1 **。 –
session_status第一次,第二次还是两者都返回1?根据http://php.net/manual/es/function.session-status.php 1表示没有活动会话。 –
在'session_unset()'和'session_destroy()'返回** 2 **之前调用'session_status()',之后返回** 1 **。然后调用'print_r($ _ SESSION);'打印会话中的所有变量。 –
你有没有试过'session_destroy();'http://php.net/manual/en/function.session-destroy.php? – Hossam
哦,是的。我有。 –
纠正我,如果我错了。中止会话似乎会关闭会话,从而阻止写入存储。我想知道是什么阻止你使用'session_set_save_handler()'来实现'SessionHandlerInterface'类吗? – frz3993