我正在嘗試做一些特定的事情。我需要比較數組中每個元素的用戶名和密碼,以找到與現有用戶的匹配。有兩個數組。有兩個數組。一個包含所有的用戶信息。另一個包含登錄嘗試。練習是在登錄嘗試匹配時打印出用戶信息。所以我需要將$ loginInfo與$ userData進行比較,以查看是否有任何登錄嘗試與存儲的用戶名和密碼匹配。PHP - 檢查數組的值
我還需要在本練習中使用substr(),md5()和strtolower()。用戶名不區分大小寫,而密碼區分大小寫。我不知道我該怎麼做,但我可以在用戶名上使用strtolower(),但我也在尋找md5哈希中的最後8個字符。不知道我是如何做到這一點。我將密碼散列的最後8個字符與登錄嘗試散列進行比較。
我覺得這會讓所有試圖幫助的人感到困惑。這顯然令我困惑。
我附上我的代碼,希望有助於更好地理解這一點。
感謝先進!
<?php
$userData = array();
$userData[] = array(
'Name' => 'Joe Banks',
'Acct' => '12345',
'Email' => '[email protected]',
'UserName' => 'Joe',
'Password' => '8e549b63',
'Active' => false);
'Password' => 'Password1'
$userData[] = array(
'Name' => 'Polly Cartwrite',
'Acct' => '34567',
'Email' => '[email protected]',
'UserName' => 'PCart',
'Password' => '91f84e7b',
'Active' => true);
'Password' => '12345'
$userData[] = array(
'Name' => 'Jake Jarvis',
'Acct' => '81812',
'Email' => '[email protected]',
'UserName' => 'jakej',
'Password' => 'd5cc072e',
'Active' => true);
'Password' => 'LetMeIn'
$userData[] = array(
'Name' => 'Kelly Williams',
'Acct' => '76253',
'Email' => '[email protected]',
'UserName' => 'kellyw',
'Password' => '2d635fc7',
'Active' => false);
'Password' => 'Kelly'
$userData[] = array(
'Name' => 'Cindy Ella',
'Acct' => '62341',
'Email' => '[email protected]',
'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);
?>
作業,對不對? – Cogicero 2013-02-19 12:03:38
我建議你使用PHP手冊來學習這門語言的基礎知識http://es.php.net/manual/es/index.php – Lobo 2013-02-19 12:04:02