2017-10-20 89 views
0

我有關聯數組中存儲的用戶名,密碼存儲在關聯數組的值中。如何使用關聯數組創建用戶名和登錄系統PHP

Array ([JOH121280 ] => John [JAN020578 ] => Jane 
[ANN151085 ] => Anna [RAHUL123058 ] => Rahul 
[BEN951357 ] => Ben) 

我想比較通過從會話存儲&顯示如果一個錯誤消息是錯誤的拍攝的用戶輸入的2個值(ID &密碼)。我不明白爲什麼我不能簡單地比較這些值。

P.S.我從文件&的值中將它放在關聯數組中。

回答

0

首先確保您的數組格式正確。然後你可以做到這一點。

$array = Array ("JOH121280" => "John", "JAN020578" => "Jane", 
"ANN151085" => "Anna", "RAHUL123058" => "Rahul", 
"BEN951357" => "Ben") ; 

$id_entered //say this variable has the entered id 
$password_entered //say this variable has the entered password 

foreach ($array as $id =>$password) 
    { 
    if($id = $id_entered && $password = $password_entered) 
     { 
     //do your thing 
     } 
    } 
0

希望你提到的這樣的事情

function loginTester() 
    { 
     $returnValue = ""; 
     $userDataArray = Array([JOH121280] => John [JAN020578 ] => Jane[ANN151085]=> Anna [RAHUL123058] => Rahul [BEN951357] => Ben) ; //Please format this to PHP 

     $userName = $_SESSION['username']; 
     $pwd = $_SESSION['Password']; 

     $userNameExists = array_key_exists($userName, $userDataArray); 
     if ($userNameExists) { 
      if ($userDataArray[$userName] == $pwd) { 
       $returnValue = "Match Success"; 
      } else { 
       $returnValue = "Password Wrong"; 
      } 

     } else { 
      $returnValue = "user name wrong"; 
     } 
     return $returnValue; 
    }