title: Reload login screen with error message after incorrect credentials entered
body: <p>I'm having an issue with some code I am trying to change.  Ignore the blatant security issues in the code, i'm not using a database in this exercise.</p>
  
  <p>Basically, I need to display an incorrect credential error message if the user inputs the wrong user/password.  I cannot figure out how to do this.  I have got it so it redirects back to the login page but I cannot get an messages to appear on that page.</p>
  
  <p>The code is as follows:</p>
  
  <p>Form on login.php</p>
  
     
  
  <pre><code>&lt;form action="login2.php" method="post"&gt;
  
        &lt;p&gt;Username: &lt;input type="text" name="username" /&gt;&lt;/p&gt;
  
        &lt;p&gt;Password: &lt;input type="password" name="password" /&gt;&lt;/p&gt;
  
        &lt;p&gt;&lt;input type="submit" value="Login" name ="sub" /&gt; &lt;/p&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>PHP on login2.php</p>
  
  <pre><code>&lt;?php
  
    session_start();
  
   // Checks the input from login.php
  
     if(isset($_POST['sub'])){
  
          $username = $_POST['username']; $password = $_POST['password'];
  
     if($username === 'test' &amp;&amp; $password === 'pass'){
  
          $_SESSION['userid'] = true; header("Location: index.php"); die();
  
      }
  
          else 
  
      if($username !== 'admin' or $password !== 'password')$userError = 'Invalid credentials';
  
      {
  
          echo "Wrong username or password";
  
             $_SESSION["loginerror"] = 1;
  
          header("Location:login.php");
  
      }
  
   }    
  
  // Allows ANY input to trigger access to the main site
  ?&gt;
  </code></pre>
  
  <p>I've looked at the solutions on this question, but they dont seem to work:
    <a href="https://stackoverflow.com/questions/18814483/incorrect-username-password-message-and-redirect">Incorrect username / password message and redirect</a></p>