Php 数组\u搜索不返回值

Php 数组\u搜索不返回值,php,Php,所以我正在用php编写这个登录脚本。我选择不使用像mysql这样的数据库,所以我将用户名存储在一个文件中,哈希存储在另一个文件中。然后它搜索用户名文件并找到用户名所在的行。然后读取哈希文件中的同一行,并比较用户输入的哈希。出于某种原因,只有当用户名是文件中的最后一个用户名或第一个用户名时,它才起作用。这意味着$inFileUsernameKey没有值,除非用户名是第一个或最后一个。这是我的密码: //Above is the code to get username and passwo

所以我正在用php编写这个登录脚本。我选择不使用像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);
    }
//以上是从html论坛获取用户名和密码并生成哈希的代码。
$usernameFileHandle=fopen(“passStuff/usernames.txt”、“r+”);
$usernameFileContent=file_get_contents(“passStuff/usernames.txt”);
$usernameFileContent=explode(“\n”,$usernameFileContent);
//在某个时候使用fopen()和fclose b/c,我将添加到帐户创建部分
fclose($usernameFileHandle);
$hashFileHandle=fopen(“passStuff/hash.txt”、“r+”);
$hashFileContent=file_get_contents(“passStuff/hash.txt”);
$hashFileContent=explode(“\n,$hashFileContent”);
//在某个时候使用fopen()和fclose b/c,我将添加到帐户创建部分
fclose($hashFileHandle);
$inFileUsernameKey=array\u search($username,$usernameFileContent);
$inFileUsername=$usernameFileContent[$inFileUsernameKey];
$i=0;
回显“用户名文件内容:
”; 而($i”; $i=$i+1; } 回声“
”; $i=0; echo“hashFileContent:
”; 而($i”; $i=$i+1; } echo“文件中的用户名:”,$inFileUsername; $inFilePassword=$hashFileContent[$inFileUsernameKey]; $inFilePassword=修剪($inFilePassword); echo“
用户名键:”,$inFileUsernameKey; echo“
在文件哈希中:”,$inFilePassword; echo“

登录:”; if(strcmp($inFilePassword,$loginInputHash)==0){ 回应“接受”; } if(strcmp($inFilePassword,$loginInputHash)!=0){ 回声“拒绝”; echo“STRCMP COMPARE:”,STRCMP($inFilePassword,$loginInputHash); }

有人知道为什么这不起作用吗?请原谅我的愚蠢。提前感谢。

fopen()
file\u get\u contents()
之前不需要
fopen()
,而且如果您正在读取行数组,也不需要
file\u get\u contents()+explode()。这里有一个简单的例子,你可以发布一个用户名文件的内容吗?我可以问一下将它们存储在不同文件中的原因吗?为什么不将
用户散列
放在一个文件中?似乎增加了太多的复杂性和失败/不同步密码的可能性,但没有安全性。我刚刚注意到其中关于fopen()/fclose()的评论。不要花费精力,而是使用
file\u put\u contents()
。使用
print\r($usernameFileContent)
打印($username)
以检查值。文件中的用户名可能有额外的字符(例如末尾的空格),或者
$username
有一些额外的字符。