Associative array with username and password(具有用户名和密码的关联数组)
本文介绍了具有用户名和密码的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将用户名和密码存储在关联数组中,我希望有一个登录系统,它可以检测错误的密码或用户名不存在。我有一个If Else代码,但是否有办法使代码更短?我在LoginValidator.php上有此代码
<?php
$users = array(
array("User" => "Test", "Password" => "123"),
array("User" => "TestUser", "Password" => "x021"),
array("User" => "Admin", "Password" => "admin"),
array("User" => "user", "Password" => "user")
);
if ($_GET['username'] == $users[0]["User"] and $_GET['password'] == $users[0]["Password"]) {
echo "Login Succesful";
} else if ($_GET['username'] == $users[1]["User"] and $_GET['password'] == $users[1]["Password"]) {
echo "Login Succesful";
} else if ($_GET['username'] == $users[2]["User"] and $_GET['password'] == $users[2]["Password"]) {
echo "Login Succesful";
} else if ($_GET['username'] == $users[3]["User"] and $_GET['password'] == $users[3]["Password"]) {
echo"Login Succesful";
}
?>
这是login.html
<div class="login">
<input type="text" placeholder="User Name" name="username"/>
<input type="password" placeholder="Password" name="password"/>
<input type="submit" value="Login"/>
</div>
推荐答案
我会构建一个检查函数,这样您就可以更好地处理它。
<?php
$users = array(
array("User" => "Test", "Password" => "123"),
array("User" => "TestUser", "Password" => "x021"),
array("User" => "Admin", "Password" => "admin"),
array("User" => "user", "Password" => "user")
);
$_GET['username'] = 'Test';
$_GET['password'] = '123';
function check($user, $pw, $users) {
$res = false;
foreach($users as $row) {
if ($row['User'] === $user && $row['Password'] === $pw) {
$res = true;
}
}
return $res;
}
echo check($_GET['username'], $_GET['password'], $users) ? 'Success' : 'Failed';
// output: Success
这篇关于具有用户名和密码的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:具有用户名和密码的关联数组


猜你喜欢
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01