【问题标题】:session_destroy() is not logging me outsession_destroy() 没有让我退出
【发布时间】:2018-05-16 22:43:23
【问题描述】:

每当我运行 logout.php 脚本然后返回到未登录的受保护页面时,我仍然会登录

注销.php

<?php

session_start(); 
session_unset();
session_destroy();
header("Location: ../index.php");
exit();
?>

登录.php

$userlogin = user_login($email, $password.$salt);
    if ($userlogin==false){
        $errors[]='Wrong email/password combination.';
    } else {
    //set the user session
        $_SESSION['UserId']=$userlogin;
        $_SESSION['LoginIP']=$_SERVER['REMOTE_ADDR'];
        $db->query("UPDATE users SET ipadd='".$_SERVER['REMOTE_ADDR']."' WHERE user_id=".$_SESSION['UserId']."");
        echo '<meta http-equiv="refresh" content="0; URL=index.php">';  

检查登录sn -p

/* Check if user is logged in or not */
function loggedin(){
return (isset($_SESSION['UserId'])) ? true : false;
}
if (loggedin()==true){
$session_user_id = $_SESSION['UserId'];
$user_data = user_data($session_user_id,'full_name','username');
$rezult =$db->query("SELECT ipadd FROM users WHERE user_id=".$_SESSION['UserId']."");
while($rez = $rezult->fetch_assoc()){
    if  ($rez['ipadd']==$_SERVER['REMOTE_ADDR']) {
    } else {
    echo '<meta http-equiv="refresh" content="0; URL=logout2.php">';
    }

}
}

看过有同样问题的帖子,但无论我尝试什么,仍然遇到同样的问题。任何建议将不胜感激!

【问题讨论】:

    标签: php session


    【解决方案1】:

    这是来自 php.net http://php.net/manual/en/function.session-destroy.php 注意:您不必从通常的代码中调用 session_destroy()。清理 $_SESSION 数组而不是销毁会话数据。

    所以你只需要 $_SESSION = null 就可以退出了。

    【讨论】:

      【解决方案2】:

      我认为在你的 index.php 文件中应该有这些行:

      if(!isset($_SESSION["session_name"])){
           header("Location: somewhere_mainpage.php");
      }
      

      最好让所有页面都有这些行。如果没有会话开始,这些行会将标题发送到另一个页面。

      【讨论】:

        【解决方案3】:

        我相信session_start();函数调用应该在用户登录数据正确的情况下在你的登录页面上,并且在你的注销PHP代码中,你应该设置

        session_destroy();unset($_SESSION['UserId'];

        注销.php:

         <?php
            session_destroy();
            /* * OR * */
            //unset($_SESSION['UserId'];
            header("Location: ../index.php");
            exit();
            ?>
        

        【讨论】:

        • 我同意。 session_start(); 应该在每一页上。我只能假设 OP 有它,因为 login.php 看起来像部分代码。
        【解决方案4】:
        <?php
        session_unset();
        session_destroy();
        header("Location: ../index.php");
        ?>
        

        应该可以,否则你可以取消设置值

        <?php
        unset($_SESSION['UserId']);  // Unsets the UserId Variable reuse for each variable 
        session_destroy();
        header("Location: ../index.php");
        ?>
        

        你试过 session_destroy() 吗?

        我也不确定你在关闭会话时是否需要 session_start(),从内存中你只需要它来启动会话

        【讨论】:

          【解决方案5】:

          我总是喜欢销毁服务器会话和客户端cookie,尝试手动覆盖所有选项以防出现任何错误。

          您可以使用以下方法在 PHP 中销毁 cookie:

          setcookie(session_name(), '', time() - 3600, $cookie_path, $cookie_domain, $cookie_secure, $cookie_httponly );
          
          
          <?php
          $cookie_path = "...";
          $cookie_domain = "...";
          $cookie_secure = "...";
          $cookie_httponly = "...";
          
          session_start();
          session_unset();
          session_destroy();
          setcookie(session_name(), '', time() - 3600, $cookie_path, $cookie_domain,$cookie_secure, $cookie_httponly );
          header("Location: ../index.php");
          exit();
          

          time() - 3600 使 cookie 在当前时间之前过期,使其失效。

          另一个调查选项是在您的注销页面上session_regenerate_id()。一些参考页面如下:

          php.net - session-regenerate-id

          https://stackoverflow.com/a/22965580/1246494

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-11-28
            • 2014-07-20
            • 1970-01-01
            • 2021-08-14
            • 2012-01-28
            • 1970-01-01
            • 1970-01-01
            • 2013-04-06
            相关资源
            最近更新 更多