我的项目的任务很简单:逐行读取文件,检查用户和密码是否存储在“ database.txt”中。 问题是“ fgets($ fd)”每次返回“ FALSE”而不是字符串,因此当我使用strpos或substr时,返回值始终为false。 这是我的代码,感谢您的帮助:
<?php
function signin(){
if($_SERVER['REQUEST_METHOD']=="POST"){
if(empty($_REQUEST['loginuser']) || empty($_REQUEST['loginpsw']))
return false;
else{
$fd = fopen("database.txt","r");
while(!feof($fd)){
$line = fgets($fd);
if(strpos($line,$_REQUEST['loginuser']) !== false){
if(substr($line,strrpos($line,",")+1) === sha1($_REQUEST['loginpsw'])){
fclose($fd);
return true;
}
}
}
fclose($fd);
return false;
}
}
else return false;
}
if(!signin()){
header("Location: index.html");
}
else {
header("Location: home.php");
}
?>
<html>
<head>
<title>Test Page</title>
<meta name="keywords" content="testpage test page keywords aristocats pompelmi cavolfiore" />
<meta name="author" content ="Alessandro Savona" />
<link rel="stylesheet" type="text/css" href="css/Style.css" />
<link href="https://fonts.googleapis.com/css?family=Bangers" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Bungee+Inline" rel="stylesheet">
</head>
<body>
<div class="topbar">
<div class="logo">
<label>We Like Pepperoni</label>
</label>
</div>
<div class="logo2">
<img src="https://www.trieste.pizza/resources/images/trieste-pizza-logo.png">
</div>
</div>
</body>
</html>
再次感谢您的帮助!
编辑:
$ fd的值为5 它位于路径/ var / www / html中 那是文件:
###########################################################
# name surname,username,password SHA1(Rowling) #
###########################################################
Harry Potter,potter,f5043f5cb1f52071e96a44303919b88179eb7bad
Ron Weasley,weasley,f5043f5cb1f52071e96a44303919b88179eb7bad
Hermione Granger,granger,f5043f5cb1f52071e96a44303919b88179eb7bad
Lord Voldemort,voldemort,f5043f5cb1f52071e96a44303919b88179eb7bad
Albus Dumbledore,dumbledore,f5043f5cb1f52071e96a44303919b88179eb7bad
Severus Snape,snape,f5043f5cb1f52071e96a44303919b88179eb7bad
Rubeus Hagrid,hagrid,f5043f5cb1f52071e96a44303919b88179eb7bad
Draco Malfoy,malfoy,f5043f5cb1f52071e96a44303919b88179eb7bad