0

我需要您对此登录脚本的帮助。当用户注册时,active数据库中的一列设置为零 (0),并且激活链接将发送到他们提供的电子邮件。单击激活链接时,如果成功,则它们各自的active列设置为 1。

但是只有当我单击提交按钮两次时,用户名和密码的正确组合才有效。

在第一次单击时,我得到以下几行

注意:未定义的索引:请记住第 37 行的 C:\wamp\www\church\login.php

尚未验证

但是当我第二次单击登录时。

<?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>";

        }
    }
}
?>
4

2 回答 2

1

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

老实说,从错误消息中应该很明显,尤其是。看到您isset在脚本中的其他任何地方都有测试代码。

于 2012-05-19T23:19:34.193 回答
0

问题在于您没有登录。而是您的代码die()在发送重定向标头后没有登录,因此继续执行该页面上的代码。此外,如果用户不选择remember,则不会发生任何事情。要修复(第一个问题),只需die(); header("location: ...");.

为什么我需要die();after header("location: ...");

header()函数将字符串(文本)发送到浏览器(或其他请求页面的程序)。但是,服务器不知道 和 之间的区别header("location: ...");header("somerandomthing: ...");因此它会继续执行代码,直到到达脚本末尾、出现错误或客户端断开连接。因此,为了防止代码继续执行,只需在每次调用die();后添加即可。 header("location: ...");

关于不检查“记住”的更新

<?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";
        }
    }
于 2012-05-19T23:24:17.173 回答