0

所以我正在用 php.ini 编写这个登录脚本。我选择不使用像 mysql 这样的数据库,所以我将用户名存储在一个文件中,而将哈希值存储在另一个文件中。然后它搜索用户名文件并找到用户名在哪一行。然后它读取哈希文件中的同一行并比较用户输入的哈希。出于某种原因,它仅在用户名是文件中的最后一个用户名或第一个用户名时才有效。这意味着 $inFileUsernameKey 没有值,除非用户名是第一个或最后一个。这是我的代码:

    //Above is the code to get username and password from html forum and generate hash.
    $usernameFileHandle = fopen("passStuff/usernames.txt", "r+");
    $usernameFileContent = file_get_contents("passStuff/usernames.txt");
    $usernameFileContent = explode("\n", $usernameFileContent);
    //Using fopen() and fclose b/c at some point I will be adding in a account creation section
    fclose($usernameFileHandle);

    $hashFileHandle = fopen("passStuff/hash.txt", "r+");
    $hashFileContent = file_get_contents("passStuff/hash.txt");
    $hashFileContent = explode("\n", $hashFileContent);
    //Using fopen() and fclose b/c at some point I will be adding in a account creation section
    fclose($hashFileHandle);

    $inFileUsernameKey = array_search($username, $usernameFileContent);
    $inFileUsername = $usernameFileContent[$inFileUsernameKey];
    $i = 0;
    echo "usernameFileContent: <br>";
    while($i < count($usernameFileContent)){
        echo "&nbsp&nbsp&nbsp&nbsp", $i, ": ", $usernameFileContent[$i], "<br>";
        $i = $i + 1;
    }
    echo "<br>";
    $i = 0;
    echo "hashFileContent: <br>";
    while($i < count($hashFileContent)){
        echo "&nbsp&nbsp&nbsp&nbsp", $i, ": ", $hashFileContent[$i], "<br>";
        $i = $i + 1;
    }
    echo "In File Username: ", $inFileUsername;
    $inFilePassword = $hashFileContent[$inFileUsernameKey];
    $inFilePassword = trim($inFilePassword);
    echo "<br>Username Key: ", $inFileUsernameKey;
    echo "<br>In File Hash: ", $inFilePassword;
    echo "<br><br>Login: ";
    if(strcmp($inFilePassword, $loginInputHash) == 0){
        echo "<div style='color: #0DFF00'>Accepted</div>";
    }
    if(strcmp($inFilePassword, $loginInputHash) != 0){
        echo "<div style='color: #FF0000'>Denied</div>";
        echo "STRCMP COMPARE: ", strcmp($inFilePassword, $loginInputHash);
    }

有谁知道为什么这不起作用?原谅我的 php 菜鸟。提前致谢。

4

0 回答 0