我正在尝试做一些具体的事情。我需要比较数组的每个元素中的用户名和密码,以找到与现有用户的匹配项。
有两个数组。一个包含所有用户信息。另一个包含登录尝试。练习是在登录尝试匹配时打印出用户信息。所以我需要将 $loginInfo 与 $userData 进行比较,以查看是否有任何登录尝试与存储的用户名和密码匹配。
在本练习中,我还需要使用 substr()、md5() 和 strtolower()。用户名不区分大小写,而密码区分大小写。我不确定我应该如何执行此操作,但我可以在用户名上使用 strtolower(),但我也在寻找 md5 哈希中的最后 8 个字符。也不知道我是怎么做到的。我将密码哈希的最后 8 个字符与登录尝试哈希进行比较。
我觉得这会让所有试图提供帮助的人感到困惑。这显然让我感到困惑。
我附上了我的代码,希望能帮助你更清楚地理解这一点。
先谢谢了!
<?php
$userData = array();
$userData[] = array(
'Name' => 'Joe Banks',
'Acct' => '12345',
'Email' => 'joe@home.com',
'UserName' => 'Joe',
'Password' => '8e549b63',
'Active' => false);
'Password' => 'Password1'
$userData[] = array(
'Name' => 'Polly Cartwrite',
'Acct' => '34567',
'Email' => 'polly@yahoo.com',
'UserName' => 'PCart',
'Password' => '91f84e7b',
'Active' => true);
'Password' => '12345'
$userData[] = array(
'Name' => 'Jake Jarvis',
'Acct' => '81812',
'Email' => 'jjar@gmail.com',
'UserName' => 'jakej',
'Password' => 'd5cc072e',
'Active' => true);
'Password' => 'LetMeIn'
$userData[] = array(
'Name' => 'Kelly Williams',
'Acct' => '76253',
'Email' => 'kw1234@yahoo.com',
'UserName' => 'kellyw',
'Password' => '2d635fc7',
'Active' => false);
'Password' => 'Kelly'
$userData[] = array(
'Name' => 'Cindy Ella',
'Acct' => '62341',
'Email' => 'washgirl@momsplace.com',
'UserName' => 'Cinders',
'Password' => '87c0e367',
'Active' => true);
'Password' => '9Kut!5pw'
// The loginInfo array contains a series of login attempts. Each attempt
// is composed of a username and password
$loginInfo = array();
$loginInfo[] = array('joe','hello');
$loginInfo[] = array('PCART','12345');
$loginInfo[] = array('jakej','letmein');
$loginInfo[] = array('KellyW','Kelly');
$loginInfo[] = array('Cinder','9Kut!5pw');
// function printUser()
// inputs:
// $user - an array containing the user's data. The expectation is that
// this array will contain the user's name, password, username,
// active status, account number and email address
// outputs:
// n/a
// This function will print out all of the information for a particular
// user in tabular format (with the exception of the password which will
// be suppressed).
function printUser($user) {
// Each user will be printed in its own row in the table
echo "<div class='tablerow'>\n";
foreach ($user as $index => $item) {
// suppress printing the password
if ($index == "Password")
continue;
// pretty print the user's status
if ($index == "Active") {
if ($item) {
$item = "active";
} else {
$item = "inactive";
}
}
// print the data in a tabledata box
echo "<div class='tabledata'>$item</div>\n";
}
// end the row
echo "</div>\n";
}
function checkLogin($loginInfo){
global $userData;
foreach($userData as $attempt) {
if($loginInfo[$attempt][0] == $userData['UserName']){
if($loginInfo[$attempt][1] == $userData['Password']){
printUser($userData);
}
}
}
}
checkLogin($loginInfo);
?>