我正在尝试在上传到 Web 域的 .PHP 文件中使用 html 和 PHP 使用关联数组制作一个简单的登录页面,但是当我尝试比较用户输入的用户名和密码时,它仅适用于第一对在关联数组中,有人可以帮我处理我的代码吗?
我已经尝试使用多种不同的方法在登录中使用关联数组,但是它只适用于第一对登录详细信息,而另外两个始终打印用户名和密码是错误的,尽管是正确的
代码:
任务 2
<p> <h1><em>Student login page </em></h1></p>
<form action= "" method="POST"> <!--The form collects the users input and sends it to the server-->
Username: <input type ="string" name="InputUsername">
<form action= "" method="POST">
</br>
Password: <input type ="string" name="InputPassword">
<input type="submit" value="Enter"> <!--submits the users input to the server -->
</form>
<?php
$Username = $_POST['InputUsername']; // collects the input from the variable "$InputUsername " to the variable "$username"
$Password = $_POST['InputPassword']; // collects the input from the variable "$InputPassword " to the variable "$Password"
$login = TestLogin($Username,$Password);
if($login == true){ and prints an assigned sentence
echo "Correct login!";
}
else {
echo "WRONG! Try again.";
}
function TestLogin($U,$P){
//use PHP Associative Array
$Students =array("PRA0015"=>"WVvy838&","KAN0025"=>"MLda725%","BOW0007"=>"YBvw487%");
foreach($Students as $username1=>$password1 ){
if( $U==$username1 & $P==$password1){
return true ;
}
else{
return false;
}
}
}
?>
一旦你点击 return false;没有回头路了。这将是该方法的结尾。相反,为条件分配一个变量并返回该变量。喜欢:
function TestLogin($U,$P){
$Students =array("PRA0015"=>"WVvy838&","KAN0025"=>"MLda725%","BOW0007"=>"YBvw487%");
$check=false;
foreach($Students as $username1=>$password1 ){
if( $U==$username1 & $P==$password1){
$check=true;
}
}
return $check;
}
foreach循环后需要去掉else然后返回false