没有SQL数据库的PHP登录问题
我试图创建一个登录页面,但遇到了一些问题。即使我知道在我的.txt文档中有正确的密码,我也无法让这段代码不返回false(尽管它只是散列的) 这是我的PHP文件,我无法停止获取,无法返回False:没有SQL数据库的PHP登录问题,php,login,Php,Login,我试图创建一个登录页面,但遇到了一些问题。即使我知道在我的.txt文档中有正确的密码,我也无法让这段代码不返回false(尽管它只是散列的) 这是我的PHP文件,我无法停止获取,无法返回False: <?php $file1 = 'userlist.txt'; $file2 = 'passlist.txt'; if ($_SERVER["REQUEST_METHOD"] == "POST") { $user = trim($_POST["usermail"]); $pas
<?php
$file1 = 'userlist.txt';
$file2 = 'passlist.txt';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = trim($_POST["usermail"]);
$pass = trim($_POST["password"]);
}
$hashedPass = "";
$arr1 = file($file1);
$arr2 = file($file2);
$userKey = array_search($user, $arr1);
if ($userKey != false) {
reset($arr2);
for ($x = 0; $x <= $userKey; $x++) {
next($arr2);
if ($x == $userKey) {
$hashedPass = current($arr2);
}
}
echo $hashedPass;
}
if (password_verify($pass, $hashedPass)) {
header("Location: worked.html"); //change this to direct user to market
}
else {
/*header("Location: index.html"); //change this to direct user back to login page with error prompt*/
print $pass;
print $hashedPass;
echo '<br>Invalid pass.';
return false;
}
?>
另外,如果你能想到我的代码中应该有什么,请告诉我非常感谢
编辑:现在更新了我的代码。仍然返回False 由于
unHash
是一个函数
,因此它没有被执行(我看不到它被调用),因此$hashedPass
没有被设置。以后,请尝试添加一些调试语句(例如,在返回false;
之前只打印$pass
和$hashedPass
)。暗中操作:在输入正确的登录组合时,您关闭了错误消息,只得到一个空白页而不是重定向
如果是这种情况,您可以使用以下代码:
<?php
$file1 = 'userlist.txt';
$file2 = 'passlist.txt';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user = trim($_POST["usermail"]);
$pass = trim($_POST["password"]);
}
$hashedPass = "";
$arr1 = file($file1);
$arr2 = file($file2);
$userKey = array_search($user, $arr1);
if ($userKey != false) {
reset($arr2);
for ($x = 0; $x <= $userKey; $x++) {
next($arr2);
if ($x == $userKey) {
$hashedPass = current($arr2);
}
}
// echo $hashedPass;
}
if (password_verify($pass, $hashedPass)) {
header("Location: worked.html"); //change this to direct user to market
}
else {
/*header("Location: index.html"); //change this to direct user back to login page with error prompt*/
print $pass;
print $hashedPass;
echo '<br>Invalid pass.';
return false;
}
?>
代码失败的原因是echo语句,它在头重定向之前执行。在头重定向之前不允许有任何输出。(关于这种行为的更多信息:)所以我应该这样做:如果(密码验证($pass,$hashedPass.unHash()){?这对我来说不起作用。还有其他问题,但我认为如果你只删除
函数unHash(){
和关闭},它们都会消失
。除非您需要函数unHash
从某处调用,否则这可能是您最好的方法。加上调试语句。我这样做了,但它仍然返回False。此时,您需要调试。或者使用调试器(不熟悉PHP可用的内容)或者只需将echo
语句添加到您的代码中,以查看is在何处按预期停止运行。如果($userKey!=false)语句,则哈希密码似乎不会从语句中消失。