0

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

知道为什么我必须单击注销按钮两次才能注销吗?

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

        <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 !-->

谢谢!


4

4 回答 4

4

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

于 2013-06-03T17:00:16.160 回答
0

直到页面重新加载后,浏览器才能识别 cookie 更改。

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

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

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

于 2013-06-03T17:06:36.890 回答
0

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

于 2013-06-03T17:03:27.960 回答
-2

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

于 2019-08-30T03:03:06.517 回答