【问题标题】:Problems logging out - have to click logout twice注销问题 - 必须单击注销两次
【发布时间】:2013-06-03 16:59:07
【问题描述】:

我希望有人可以帮助解决从直觉上看应该很简单的问题,但我无法找到答案。出于某种原因,当我的用户注销时,他必须单击两次注销才能使其正常工作。我读过这个:

Any idea why I have to click the logout button twice to logout?

但它似乎不起作用。这是我的注销按钮代码。有什么想法吗?谢谢!

        <div id="loginStatusWrap">
            <div id="loginStatus">
    <?php
    include('includes/APILogin.php');


    if ( isset( $_POST['logout_btn'] ) )
    {
        unset($_COOKIE['kp_emailID']);
        unset($_COOKIE['kp_pass']);
        session_destroy();
    }


    // Check for login cookie - skip if session is available
    if ( isset($_COOKIE['kp_emailID']) && isset($_COOKIE['kp_pass']) && !isset($_SESSION['kp_accountID']) )
    {
        $username = $_COOKIE['kp_emailID'];
        $pass = $_COOKIE['kp_pass'];
        $get_account_parameters = array(
                               'session' => $session_id,
                               'module_name' => 'kd_kp',
                               'query' => "kd_kp_cstm.username_c = '" . $_COOKIE['kp_emailID'] . "'",
                               'order_by' => "",
                               'offset' => '0',
                               'select_fields' => array(
                                                    //'username_c',
                                                    //'password_c',
                                                    //'id',
                                                    //'name',
                                                    ),
                               'link_name_to_fields_array' => array( ),
                               'max_results' => '1',
                               'deleted' => '0',
                               'Favorites' => false,
                               );
        $get_account_result = call('get_entry_list', $get_account_parameters, $url);
        //echo '<pre>'; print_r($get_account_result); echo '</pre>';

        if ( $_COOKIE['kp_pass'] != $get_account_result->entry_list[0]->name_value_list->password_c->value )
        {
            // not logged in
            session_unset();
            session_destroy();
            ?>
                <div id="loginForm">
                    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                        <input type="text" placeholder="EMAIL" value="<?=$_COOKIE['kp_emailID']?>" name="signIn_email" id="signIn_email"/>
                        <input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
                        <input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
                        <!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
                    </form>
                </div><!-- //logInForm -->
            <?php
        } else {
            // is logged in
            $_SESSION['kp_accountID'] = $get_account_result->entry_list[0]->name_value_list->id->value;
            $_SESSION['kp_name'] = $get_account_result->entry_list[0]->name_value_list->name->value;
            // set cookies
            $hour = time() + 3600;
            setcookie("kp_emailID", $get_account_result->entry_list[0]->name_value_list->email1->value, $hour, "/", "kp.com");
            setcookie("kp_pass", $get_account_result->entry_list[0]->name_value_list->password_c->value, $hour, "/", "kp.com");
            ?>
            <div id="loginForm">
                <h1>WELCOME, <?=$get_account_result->entry_list[0]->name_value_list->name->value?>
                <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
                <input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
                </form>
            </div>
            <?php
        }
    }

    //if the login form is submitted 
    if ( isset( $_POST['login_btn'] ) )
    {
        // checks it against the database
        if ( !get_magic_quotes_gpc() )
        {
            $_POST['signIn_email'] = addslashes($_POST['signIn_email']);
        }

        $get_account_parameters = array(
                                       'session' => $session_id,
                                       'module_name' => 'kd_kp',
                                       'query' => "kd_kp_cstm.username_c = '".$_POST['signIn_email']."'",
                                       'order_by' => "",
                                       'offset' => '0',
                                       'select_fields' => array(
                                                    //'username_c',
                                                    //'password_c',
                                                    //'id',
                                                    //'name',
                                                    ),
                                       'link_name_to_fields_array' => array( ),
                                       'max_results' => '1',
                                       'deleted' => '0',
                                       'Favorites' => false,
                                       );
        $get_account_result = call('get_entry_list', $get_account_parameters, $url);

        //Gives error if user dosen't exist
        if ( $get_account_result->result_count == 0 )
        {
            ?>
            <div id="loginForm">
                <a href='/sign-up.php'>Click Here to Register</a>
            </div>
            <?php
        }

        $_POST['pass'] = md5( stripslashes($_POST['signIn_pwd']) );

        //gives error if the password is wrong
        if ( $_POST['pass'] != stripslashes($get_account_result->entry_list[0]->name_value_list->password_c->value) )
        {
            //if ( !isset($get_account_result->entry_list[0]->name_value_list->password_c->value) )
            {
            ?>
            <div id="loginForm">
                    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                    <input type="text" placeholder="EMAIL: TRY AGAIN" name="signIn_email" id="signIn_email"/>
                    <input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
                    <input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
                    <!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
                </form>
            </div><!-- //logInForm -->
            <?php
            }
            //echo '<pre>'; print_r($_POST); echo '</pre>';
        } else {
            // if login is ok then we update session vars
            $_SESSION['kp_emailID'] = stripslashes($_POST['signIn_email']);
            //$_SESSION['kp_pass'] = $_POST['pass'];
            $_SESSION['kp_accountID'] = $get_account_result->entry_list[0]->name_value_list->id->value;
            $_SESSION['kp_name'] = $get_account_result->entry_list[0]->name_value_list->name->value;
            ?>
            <div id="loginForm">
                <h1>WELCOME, <?=$get_account_result->entry_list[0]->name_value_list->name->value?></h1>
             <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
                <input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
                </form>
          </div><!-- //logInForm -->
            <?php
        }
    } else {
        // check for active session
        if ( isset($_SESSION['kp_accountID']) )
        {
        ?>
        <div id="loginForm">
            <h1>WELCOME, <?=$_SESSION['kp_name']?></h1>
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
            <input type="submit" value="LOG OUT" name="logout_btn" id="logout_btn" />
            </form>
        </div>
        <?php
        } else {
        // if they are not logged in
        ?>
        <div id="loginForm">
            <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                <input type="text" placeholder="EMAIL" name="signIn_email" id="signIn_email"/>
                <input type="password" placeholder="PASSWORD" id="signIn_pwd" name="signIn_pwd" />
                <input type="submit" value="SIGN IN" name="login_btn" id="login_btn" />
                <!--input type="submit" value="SIGN UP" name="signup_btn" id="signup_btn" /-->
            </form>
        </div><!-- //logInForm -->
    <?php
        }
    }

    //echo '<div style="float:left;"><pre>'; print_r($_POST); echo '</pre></div>';
    //echo '<div style="float:left;"><pre>'; print_r($_SESSION); echo '</pre></div>';
    //echo '<div style="float:left;"><pre>'; print_r($_COOKIE); echo '</pre></div>';
    ?>

              <div class="clear"></div>
            </div><!-- //loginStatus -->
        </div><!-- //loginStatusBar -->
    <!-- END loginbar.php !-->

谢谢!


【问题讨论】:

  • 请提供更多代码 - 否则我们只能猜测出了什么问题。
  • 我已向此页面提供了完整代码 -
  • 我会首先在每个 if 语句中的文件中包含一些日志记录(如果您没有好的调试器)。将 cookie/其他变量的相关值写入文件并指定它们来自哪个条件。 $_POST['logout_btn'] 例如每次用户 clikcs 注销时都会执行吗?

标签: php


【解决方案1】:

可能是因为在显示用户登录的页面部分加载之前,此代码没有运行。因此,HTML 显示他已登录,但实际上他没有。

【讨论】:

  • +1,HTML 可能位于 if 语句下方。我会尝试的一种方法是header("Location: this_page or index.php") 紧跟在session_destroy 之后。
【解决方案2】:

在页面重新加载之前,浏览器无法识别 cookie 更改。

您的第一次点击是取消设置 cookie,但用户仍处于登录状态。
您的第二次点击是“重新加载”页面,此时 cookie 已取消设置,并且似乎可以正常工作。

添加重定向到同一页面(在取消设置和销毁会话之后)以模拟刷新,您的按钮应该可以工作。

附言
这是 cookie 工作原理的简化版本,但它基本上是准确的。 由于您要在服务器上取消设置 cookie,因此您必须再次请求页面以获取不包含旧 cookie 信息的新 http 标头。这就是重新加载的作用。

【讨论】:

    【解决方案3】:

    销毁会话后,重定向到登录页面,使页面刷新。

    【讨论】:

    • 我把 header("location: index.php");但它不会在注销时重定向...
    • 写 ob_start();在注销页面顶部
    【解决方案4】:

    如果您通过FB登录第三方网站,您必须注销两次才能完成注销。

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 2019-01-16
      • 2018-12-04
      • 2015-01-04
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2015-09-06
      • 2012-05-23
      相关资源
      最近更新 更多