简体   繁体   English

单击“提交”按钮两次,仅登录有效

[英]Log-in Only Works When Submit button is clicked Twice

Please I need your help with this log-in script. 请使用此登录脚本获取您的帮助。 When a user registers, a column active in the database is set to zero (0), and an activation link is sent to the email they provided. 用户注册时,数据库中active的列将设置为零(0),并且激活链接将发送到他们提供的电子邮件。 When the activation link is clicked and if successful, their respective active column is set to 1. 单击激活链接后,如果成功,则它们各自的active列将设置为1。

But a correct combination of username and password only works when i click the submit button twice. 但是,只有当我单击两次提交按钮时,用户名和密码的正确组合才有效。

On the first click i get the following lines 第一次单击,我得到以下几行

Notice: Undefined index: remember in C:\\wamp\\www\\church\\login.php on line 37 注意:未定义的索引:请记住在C:\\ wamp \\ www \\ church \\ login.php中的第37行

Not Verified Yet 尚未验证

But when I click the second time the log-in works. 但是,当我第二次单击时,登录有效。

<?php
if (isset($_POST['submit'])) {
    $username = trim($_POST['username']);
    $username = strip_tags($_POST['username']);
    $username = htmlentities($_POST['username']);
    $username = stripslashes($_POST['username']);
    $username = mysql_real_escape_string($_POST['username']);
    $password = sha1($_POST['password']);

    if (isset($_POST['username']) && isset($_POST['password'])) {
        $query = mysql_query("SELECT username, password
                           FROM USERS
                           WHERE username = '$username'
                           AND password = '$password'") or die (mysql_error());

        $user = mysql_num_rows($query);
        if ($user == 1) {
            $row = mysql_fetch_assoc($query);
            if ($row['active'] == 1) {
                $_SESSION['logged_username'] = $username;
                if ($_POST['remember']) {
                    setcookie("CookieUser", $_SESSION['logged_username'], time() + 60 * 60 * 24 * 100, "/");
                    setcookie("CookiePass", $password, time() + 60 * 60 * 24 * 100, "/");

                    header('Location: http://127.0.0.1/church/index.php?id=1');
                }
            }
            if ($row['active'] !== 1) {
                echo "Not Verified Yet";
            }
        }
        else {
            echo "<div id='content' >";
            echo "<div class='OpenError' >";
            echo "Username & Password Combination Is Wrong";
            echo "</div>";
            echo "</div>";

        }
    }
}
?>

Change if ($_POST['remember']) { to if (isset($_POST['remember']) && $_POST['remember']) { if ($_POST['remember']) {更改为if (isset($_POST['remember']) && $_POST['remember']) {

Honestly, it should be very obvious from the error message, esp. 老实说,从错误消息,尤其是错误消息中应该很明显。 seeing as you have the isset test code everywhere else in your script. 看到脚本中其他任何地方都有isset测试代码。

The issue isn't that you aren't being logged in. It's that your code does not die() after sending the redirect header and therefore continues executing the code on that page. 问题不是您没有登录。这是您的代码在发送重定向标头后没有die() ,因此继续在该页面上执行代码。 Also, if the user does not select remember , nothing happens. 同样,如果用户没有选择“ remember ,则什么也不会发生。 To fix (the first issue), simply add die(); 要解决(第一个问题),只需添加die(); immediately after header("location: ..."); 紧跟在 header("location: ..."); .

Why do I need die(); 为什么我需要die(); after header("location: ..."); header("location: ..."); ?

The header() function sends a string (text) to the browser (or other program requesting the page). header()函数将字符串(文本)发送到浏览器(或其他请求页面的程序)。 However, the server doesn't know the difference between header("location: ..."); 但是,服务器不知道header("location: ...");之间的区别header("location: ..."); and header("somerandomthing: ..."); header("somerandomthing: ..."); , so it continues executing code until it either reaches the end of the script, an error, or the client disconnects. ,因此它将继续执行代码,直到到达脚本末尾,错误或客户端断开连接为止。 So, to prevent code execution from continuing, simply add die(); 因此,为防止代码执行继续进行,只需添加die(); after every header("location: ..."); 每个 header("location: ..."); call. 呼叫。

Update regarding not checking 'remember' 关于不选中“记住”的更新

<?php
if ($user == 1) {
        $row = mysql_fetch_assoc($query);
        if ($row['active'] == 1) {
            $_SESSION['logged_username'] = $username;
            if ($_POST['remember']) {
                setcookie("CookieUser", $_SESSION['logged_username'], time() + 60 * 60 * 24 * 100, "/");
                setcookie("CookiePass", $password, time() + 60 * 60 * 24 * 100, "/");

            }
            // Redirect even if not 'remembered'
            header('Location: http://127.0.0.1/church/index.php?id=1');
            die;
        }
        if ($row['active'] !== 1) {
            echo "Not Verified Yet";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM