想做一个简单的PHP登陆 界面是task2.html都可以不用管
主要是PHP文件 已知一个passwd.txt上是用户帐号密码
user1:passwd1
user2:passwd2
user3:passwd3
这样的形式给出的 这个PHP文件能判断passwd.txt里的信息就可以了
<?php
$passwd = $_POST['passwd'];
$user = $_POST['user'];
if (!checkPasswd($passwd,$user)){
echo "<h2> Wrong password $user . Try again.</h2>";
include('task2.html');
}
else{
echo "<h2> Welcome on board $user !</h2>";
}
function checkPasswd($passwd,$user){
if(!$fh = fopen("passwd.txt", "r")) return false;
while(!feof($fh)){
$line = trim(fgets($fh));
//your code goes below this line
$x = explode(":",$line);
if($user == $x[0] && $passwd == $x[1])
return true;
else
return false;
}
fclose($fh);
return false;
}
?>
</body>
</html>
我是想用 : 把帐号和密码分开来对比但实现不了不知道哪里错了 希望高手帮帮忙
我把下面的函数改了一下你看看,你写的有一点错误!当函数中遇到return时.此函数就跳出不再执行了!相当于你写了个exit;所以你如果文本中有两个以上的用户名,第二个用户名永远检测不到.
改过后测试可用
function checkPasswd($passwd,$user){
$flag=false;
if(!$fh = fopen("passwd.txt", "r")) return false;
while(!feof($fh)){
$line = trim(fgets($fh));
//your code goes below this line
$x = explode(":",$line);
if($user == $x[0] && $passwd == $x[1])
{
$flag=true;
break;
}
else
{
$flag=false;
}
}
fclose($fh);
return $flag;
}