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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?