2013-02-19 72 views
0

我正在嘗試做一些特定的事情。我需要比較數組中每個元素的用戶名和密碼,以找到與現有用戶的匹配。有兩個數組。有兩個數組。一個包含所有的用戶信息。另一個包含登錄嘗試。練習是在登錄嘗試匹配時打印出用戶信息。所以我需要將$ 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); 
?> 
+5

作業,對不對? – Cogicero 2013-02-19 12:03:38

+0

我建議你使用PHP手冊來學習這門語言的基礎知識http://es.php.net/manual/es/index.php – Lobo 2013-02-19 12:04:02

回答

0

您需要兩個循環來比較2個數組的值。試試這個:

for($i=0;$i<count($loginInfo);$i++){ 
    foreach($userData as $attempt) { 
     if($loginInfo[$i][0] == $attempt['UserName']){ 
      if($loginInfo[$i][1] == $attempt['Password']){ 
       printUser($attempt); 
      } 
     } 
    } 
} 
0
function checkLogin($storage, $input){ 
    foreach ($storage as $data){ 
    if ((strtolower($data['Name']) === strtolower($input[0])) && (md5($data['Password']) === md5($input[1]))) 
     return true ; 
    } 
    return false ; 
} 

$user_logged = checkLogin($userData, $_POST /* Or another source */) ; 

var_dump($user_logged) ; 

我不知道爲什麼你需要最後8個字符的散列進行比較,但如果你願意,我可以編輯我的答案。

但是,如果你仍然需要它,改變

(md5($data['Password']) === md5($input['Password'])) 

(substr(md5($data['Password']), -7)) === (substr(md5($input['Password']), -7)) 

要嘗試登錄的所有用戶:

$logged_users = array() ; 
foreach ($loginInfo as $info){ 
    if (checkLogin($userData, $info){ 
    $logged_users[] = $info ; 
    } 
} 

var_dump($logged_users) ; 
+0

我需要指出2個密碼字段實際上並不在代碼中。實際密碼(第二個字段)已被註釋掉,僅用於向我們顯示密碼是什麼。我不知道這是否有什麼區別。 – user1833777 2013-02-19 13:22:03

+0

@ user1833777編輯我的代碼。嘗試一下。 – vikingmaster 2013-02-19 13:30:57

0

我注意到你有2個 「密碼」 字段您的用戶數據數組中,第一個密碼字段顯示爲第二個密碼字段的md5散列的最後8個字符,如果dat a以這種方式提供給你,你不能用它做很多事情(並且必須自己得到哈希),但只要兩個密鑰相同,第二個密碼值就會覆蓋第一個密碼。

您通過假設用strtolower功能,可用於確保用戶名是否正確不區分大小寫,比較它們

strtolower($loginInfo[$attempt][0]) === strtolower($userData['UserName']) 

的時候才使用它的嘗試的用戶名和用戶數據的用戶名都MD5將被用來獲取密碼的哈希值,這樣就可以使用它,你需要的任何理由,就像

$hash = md5($loginInfo[$attempt][1]; 

的SUBSTR將有助於你得到的最後8個字符的MD5散列,使用該函數像這樣:

string substr (string $string , int $start [, int $length ]) 

不想毀掉你所有的功課,但檢查http://php.net/manual/en/function.substr.php,你會發現你的答案在負面啓動的例子。