为什么我的PHP7.1脚本说登录凭据即使正确也不正确?

为什么我的PHP7.1脚本说登录凭据即使正确也不正确?,php,login,credentials,Php,Login,Credentials,我正在用PHP编写一个登录脚本,几乎所有的东西都可以正常工作,如果我创建了一个帐户,它就可以工作,但是如果我尝试使用该帐户的凭据登录。 我收到了凭证错误的警报。 我还将密码保存在一个文本文件中,我同意你的说法,这很愚蠢,但这是学校作业的密码,所以没关系 我个人认为我的登录脚本有问题,但我不确定。(因为这是为了上学,我需要尽可能靠近代码布局。) 代码如下: signup.php <?php if (isset($_POST["submit"])) { $username = html

我正在用PHP编写一个登录脚本,几乎所有的东西都可以正常工作,如果我创建了一个帐户,它就可以工作,但是如果我尝试使用该帐户的凭据登录。 我收到了凭证错误的警报。 我还将密码保存在一个文本文件中,我同意你的说法,这很愚蠢,但这是学校作业的密码,所以没关系

我个人认为我的登录脚本有问题,但我不确定。(因为这是为了上学,我需要尽可能靠近代码布局。)

代码如下:

signup.php

<?php
if (isset($_POST["submit"])) {
    $username = htmlspecialchars($_POST["usrname"]);
    $email = htmlspecialchars($_POST["mail"]);
    $password = htmlspecialchars($_POST["pass"]);
    $passwordConfirm = htmlspecialchars($_POST["passconfirm"]);

    trim($username);
    trim($email);

    function checkUserData() {
        // things to do .A
        // things to do .B
        // things to do .C 

        // check if passwords are the same #### this doesn't seem to work correct. Why not?
        if ($password != $passwordConfirm) {
            echo "<script>alert('passwords are not matching');
            location.href='signup.html';
            </script>";
            return false;
        } 

        elseif ($password == $passwordConfirm) {
             return true;
        }

    }

    if (checkUserData()) {
        // saving user
        $file = fopen("users.txt", "ab");
        if (!$file) {
            echo "Couldn't open file!";
        }

        $profile = $username . "*" . $password . "*" . $email . "\n";

        fwrite($file, $profile, strlen($profile));

        if (fclose($file)) {
            echo "<script>alert('Account is created');
            location.href='login.html';
            </script>";
        }

        elseif (!fclose($file)) {
            echo "Couldn't close file!";
        } else {
            echo "An error ocurred while creating an account, try again later.";
        }
    }

}
?>

似乎$account数组的索引从0开始。
将第13行登录文件中的代码更改为

if ($account[0] == $username && $account[1] == $password) 
我们应该解决它


您使用trim()的方式没有效果。但是您使用
htmlspecialchars()
的方式可能会产生效果。不要弄乱用户ID和密码,使用绑定的和参数化的查询和
密码\u hash()
$password
$passwordConfirm
变量在
checkUserData()
函数中不存在您不将密码传递给函数。它将比较null和null。请尝试输出
$account
$username
$password
的值。还可以尝试输出
var_dump($account[1]=$username)
和带有密码的文件。
if ($account[0] == $username && $account[1] == $password)