title: Array Based Login - PHP
body: <p>I wrote a script which verifies login detail based on array concept. It's not db based. Everything is working fine except one thing. Actually i have stored two login credentials in an array called $users. </p>
  
  <p>If the user uses first login detail then login is always failed and prints message - "Wrong username or password" because the script matches the combination of each username and password with the incoming post data. And when user uses first credential, then the second credential gets failed and that's why we get "Wrong username or password". But if the user uses second login credential, then there is no problem and we get - "Login successful". </p>
  
  <p>I know the reason why this is happening so, but couldn't resolve it yet. One common workaround could be to use header/redirection upon successful login so that wrong message for second login credentials dont appear. </p>
  
  <p>But i want to print both the message according to login credential used - means successful login message should appear whether the user uses either first login credential or second one, and failed login message should appear if user doesn't use either of two login credentials. Here is my code -</p>
  
  <pre><code>&lt;?php
  $users = array();
  $users["admin"] = "admin";
  $users["test"] = 12345;
  echo "&lt;pre&gt;";
  print_r($users);
  echo "&lt;/pre&gt;";
  if(isset($_POST['btnlogin']))
  {
      $uname = $_POST['txtuname'];
      $pass = $_POST['txtpass'];
      foreach($users as $username =&gt; $password) 
      {
          if($username == $uname &amp;&amp; $password == $pass) 
          {
              $msg = "&lt;p&gt;Login successful&lt;/p&gt;";
          }
          else
          {
              $msg = "&lt;p&gt;Wrong username or password&lt;/p&gt;";
          }
      }
      echo $msg;
  }
  ?&gt;
  &lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Array Based Login&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post" action=""&gt;
  &lt;input type="text" name="txtuname" /&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;input type="password" name="txtpass" /&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;input type="submit" name="btnlogin" value="Login" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>