title: Part of PHP only runs if the form is submitted twice
body: <p>In a very basic login page I set up an error status to show if the form was submitted with an empty pin or an invalid pin was entered but it only functions if I submit the form twice with the same error.</p>
  <p>Where am I going wrong here?
  Or else what would be  a better way to achieve this?</p>
  <pre><code>&lt;?php
  
  if (session_status() === PHP_SESSION_NONE) {
      session_start();
  }
  
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Title&lt;/title&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  &lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;180x180&quot; href=&quot;/apple-touch-icon.png&quot;&gt;
  &lt;link rel=&quot;icon&quot; type=&quot;image/png&quot; sizes=&quot;32x32&quot; href=&quot;/favicon-32x32.png&quot;&gt;
  &lt;link rel=&quot;icon&quot; type=&quot;image/png&quot; sizes=&quot;16x16&quot; href=&quot;/favicon-16x16.png&quot;&gt;
  &lt;link rel=&quot;manifest&quot; href=&quot;/site.webmanifest&quot;&gt;
  &lt;link rel=&quot;mask-icon&quot; href=&quot;/safari-pinned-tab.svg&quot; color=&quot;#5bbad5&quot;&gt;
  &lt;meta name=&quot;msapplication-TileColor&quot; content=&quot;#da532c&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;Page Title&lt;/h1&gt;
  
  &lt;div class=&quot;smallcontainer&quot;&gt;
    &lt;form method=&quot;post&quot; action=&quot;&quot;&gt;
      
      &lt;div class=&quot;row&quot;&gt;
          
          &lt;div class=&quot;col-20&quot;&gt;
          &lt;label for=&quot;id&quot;&gt;Driver Pin&lt;/label&gt;
          &lt;/div&gt;
          &lt;div class=&quot;col-80&quot;&gt;
          &lt;input type=&quot;text&quot; name=&quot;id&quot; &gt;
          &lt;/div&gt;
      &lt;/div&gt;
      
      &lt;div class=&quot;row&quot;&gt;
          &lt;div class=&quot;col-100 error&quot;&gt;
              &lt;?= @$_SESSION['status'] ?&gt;
          &lt;/div&gt;
          &lt;div class=&quot;col-100&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;submit&quot;&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      
    &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  
  if (isset($_POST['submit'])) {
      
      $id = trim($_POST['id']);
          
      if (empty($_POST['id'])){
      $_SESSION['status'] = 'Enter a Driver Pin';
      }
      
      // if (!strlen($id)) {
      // $_SESSION['status'] = 'Enter a Driver Pin';
      // //die('Please enter Driver Pin');
      // }
      
      $success = false;
          
      $handle = fopen(&quot;users.csv&quot;, &quot;r&quot;);
          
      while (($data = fgetcsv($handle)) !== FALSE) {
          if ($data[0] == $id) {
          $success = true;
          $_SESSION['displayname'] = $data[1];
          break;
          } 
      }
      
      fclose($handle);
      
      if ($success) {
          $_SESSION['allow'] = '';
          header('Location: ./');
      } else {
          $_SESSION['status'] = 'Invalid Driver Pin - Try again';
      }
  }
  
  ?&gt;
  </code></pre>