title: Why is it that my $_SESSION or my 'if' statement won't trigger when split over two pages?
body: <p>I am trying to make an email form that when an error occurs it displays a flash error message. I have the process split over two pages, the first is the contact.php page where the form is and the other is the validation.php page where the validation and email 'mail()' function is. I link the pages via a 'require_once("validation.php");' at the top of the contact.php page. </p>
  
  <p>When I have them split over these two pages the flash message won't appear but when I have all of the code on one page only, the contact.php page, it does work. However, even though this problem is happening the email does send when the form is filled in, so I know the linking of the pages is working and some of the code is executing.</p>
  
  <p>Any idea why this is occurring?</p>
  
  <p>Here is a simplified version of my code: </p>
  
  <p>contact.php: </p>
  
  <pre><code>&lt;?php 
  require_once("validation.php");
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;title&gt;...&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;main class="container"&gt;
              &lt;div class="form-container"&gt;
              &lt;?php
  
                  echo '&lt;pre&gt;'.print_r($_POST).'&lt;/pre&gt;';
                  echo '&lt;pre&gt;'.print_r($_SESSION['error']).'&lt;/pre&gt;';
                  if ( isset($_SESSION['error']) ) {
                      echo('&lt;p style="color: red;"&gt;'.$_SESSION['error']." 
                      &lt;/p&gt;\n");
                      unset($_SESSION['error']);
                  }
  
               ?&gt;
  
              &lt;form action="contact.php" method="POST"&gt;
                  &lt;label for="name"&gt;Enter your name: &lt;/label&gt;&lt;br&gt;
                  &lt;input type="text" name="name"&gt;&lt;br&gt;
                  &lt;label for="email"&gt;Enter your email: &lt;/label&gt;&lt;br&gt;
                  &lt;input type="email" name="email"&gt;&lt;br&gt;
                  &lt;label for="subject"&gt;Subject line: &lt;/label&gt;&lt;br&gt;
                  &lt;input type="text" name="subject"&gt; 
                  &lt;br&gt;
                  &lt;label for="message"&gt;Message: &lt;/label&gt;&lt;br&gt;                                      
                  &lt;textarea name="message" cols="75" rows="10"&gt; 
                  &lt;/textarea&gt;&lt;br&gt;
                  &lt;input type="submit" value="Submit"&gt;
  
              &lt;/form&gt;
               &lt;/div&gt;&lt;!-- .form-container --&gt;
          &lt;/main&gt;
         &lt;/body&gt;
        &lt;/html&gt;
  </code></pre>
  
  <p>validation.php:</p>
  
  <pre><code>&lt;?php 
   session_start();
  
  
   if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
      $name = test_input($_POST["name"]);
      $email = test_input($_POST["email"]);
      $subject = test_input($_POST["subject"]);
      $message = test_input($_POST["message"]);
  
  
      if ( strlen($name) &lt; 1 || strlen($email) &lt; 1 || strlen($subject) &lt; 1 || 
  strlen($message) &lt; 1) {
  
          $_SESSION['error'] = "All fields are required";
          header("Location: contact.php");
          return;
  
      } else {
  
          $to = "example@example.com";
          $name = $_POST['name'];
          $email = $_POST['email'];
          $subject = $_POST['subject'];
          $message = $_POST['message'];
          $headers = "From: $name &lt;$email&gt;";
  
          mail($to, $subject, $message, $headers);
  
       }
  }
  
  function test_input($data) {
      $data = trim($data);
      $data = strip_tags($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
  }
  ?&gt;
  </code></pre>