douquanhui5735 2017-04-15 19:03
浏览 27
已采纳

too long

I know this question has many duplicates, but I tried several of them and none of those have been answered.

Here is my code for 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');
?>

But the session variables are just too "stubborn" to be removed. Neither session values are being cleared not the session variables are being removed. Object $dbh is being unset but not $_SESSION['username'];

Another unrelated problem, despite I am setting the LoggedIn = 0, in my SQL query, it just stays as 1 in database. LoggedIn field is of type 'bit'. SessionID field is set to blank though.

Any solutions please?

EDIT:

Removed echo $dbh->error as it was unnecessary.

EDIT 2:

Added session_destroy() as suggested by Hossam Magdy.

  • 写回答

3条回答 默认 最新

  • drip5880 2017-04-15 20:34
    关注

    I don't know why, but the code for destroying the sessions was somehow not working in logout.php. It worked in index.php and other files, but will all sorts of unpredictable behavior.

    Found a workaround to circumvent the problem. The logout.php has code as below:

    <?php
        session_start();
        $_SESSION['logout'] = TRUE;
        header('location:index.php');
    ?>
    

    And add this code to 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();
        }
    ?>
    

    It may not be a standardized solution, but the code works for me every time, with no unpredictable behavior.

    Thanks everyone for sharing their ideas.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用