简体   繁体   中英

How to Create a username & login system with an associative array PHP

I have the usernames stored in the key of the associative array & the password stored in the value of the associative array.

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

I would like to compare the 2 values (ID & password) entered by the user taken from the session storage & display an error message if it is wrong. I don't understand why I cannot simply compare the values.

PS I got the values from a File & put it in the associative array.

First make sure your array in in correct format. Then you can do this.

$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
      }
   }

Hope you mentioned something like this

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM