title: How to keep session variables between pages with forms?
body: <p>I'm writing a program that has 3 pages. </p>
  
  <p>On page 1 there is an option for the user to select a quantity of a breakfast product he wants to purchase. After selecting a quantity the user hits the submit button, if the user is not registered, it will take him to Page 2 for him to register. If the user is registered, it will direct them to Page 3.</p>
  
  <p>However, if the user goes to Page 2 first and does not have a quantity selected from Page 1 it will redirect him to Page 1 after he registers and press submit, and then once they select a quantity and hit submit on Page 1 it will go to Page 3.</p>
  
  <p>I'm struggling to maintain my session variables between the pages because two of them have forms that get overwritten if the user ever goes back to that page.</p>
  
  <p>Page 1:</p>
  
  <pre><code>&lt;?php 
  session_start();
      $_SESSION['name']= $_POST['name'];
      $_SESSION['email']= $_POST['email'];
      $platter_quantity = $_SESSION['platter_quantity'];
      $yogurt_quantity = $_SESSION['yogurt_quantity'];
      $waffles_quantity = $_SESSION['waffles_quantity'];
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;head&gt;
      &lt;title&gt;Product Page&lt;/title&gt;
      &lt;link rel="stylesheet" type"text/css" href="settings.css"&gt;
  &lt;/head&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;ul&gt;
    &lt;li&gt;&lt;a class="active" href="product.php"&gt;Product&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="registration.php"&gt;Registration&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="invoice.php"&gt;Invoice&lt;/a&gt;&lt;/li&gt;
    &lt;li style="float:right"&gt;&lt;a href="registration.php"&gt;Login&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;?php
  $action = '';
  if (!empty($_SESSION['name']) or !empty($_SESSION['email'])) {
      $action = "invoice.php";
      }
  else {
      $action = "registration.php";
      }
  ?&gt;
  
  &lt;form action="&lt;?php echo $action; ?&gt;" method="post"&gt;
  &lt;div class="container"&gt;
      &lt;img src="images/platter.jpg" alt="Breakfast Platter" style="float: left; width: 400px; height: 300px;";&gt;
      &lt;h1&gt;Breakfast Platter&lt;/p&gt;
      &lt;p&gt;The breakfast platter option comes with two fried eggs, four pancakes, and a bunch of bacon.&lt;/p&gt;
      Quantity: &lt;input type="number" name="platter_quantity" min="0"&gt;
      &lt;p value="10.99" name="platter_price"&gt;Price: $10.99&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div class="container"&gt;
      &lt;img src="images/yogurt.jpg" alt="Yogurt Parfait" style="float: left; width: 400px; height: 300px;"&gt;
      &lt;h1&gt;Yogurt Parfait&lt;/p&gt;
      &lt;p&gt;The yogurt parfait option comes with two cups of yogurt, oats, and a mixture of berries.&lt;/p&gt;
      Quantity: &lt;input type="number" name="yogurt_quantity" min="0"&gt;
      &lt;p value="6.99" name="yogurt_price"&gt;Price: $6.99&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div class="container"&gt;
      &lt;img src="images/waffles.jpg" alt="Waffles" style="float: left; width: 400px; height: 300px;";&gt;
      &lt;h1&gt;Waffles&lt;/p&gt;
      &lt;p&gt;The waffles option comes with two buttermilk waffles with butter and syrup.&lt;/p&gt;
      Quantity: &lt;input type="number" name="waffles_quantity" min="0"&gt;
      &lt;p value="$4.99" name="waffles_price"&gt;Price: $4.99&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;br&gt;
      &lt;button class="button" type="submit" name="submit"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Page 2:</p>
  
  <pre><code>&lt;?php
  session_start();
      $name = $_SESSION['name'];
      $email = $_SESSION['email'];
      $_SESSION['platter_quantity'] = $_POST['platter_quantity'];
      $_SESSION['yogurt_quantity'] = $_POST['yogurt_quantity'];
      $_SESSION['waffles_quantity'] = $_POST['waffles_quantity'];
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;head&gt;
      &lt;title&gt;Registration Page&lt;/title&gt;
      &lt;link rel="stylesheet" type"text/css" href="settings.css"&gt;
  &lt;/head&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;ul&gt;
    &lt;li&gt;&lt;a href="product.php"&gt;Product&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a class="active" href="registration.php"&gt;Registration&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="invoice.php"&gt;Invoice&lt;/a&gt;&lt;/li&gt;
    &lt;li style="float:right"&gt;&lt;a href="registration.php"&gt;Login&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;br&gt;
  
  &lt;?php
  $action = '';
  if (!empty($_SESSION['platter_quantity']) or !empty($_SESSION['yogurt_quantity']) or !empty($_SESSION['waffles_quantity'])) {
      $action = "invoice.php";
      }
  else {
      $action = "product.php";
      }
  ?&gt;
  
  &lt;form action="&lt;?php echo $action; ?&gt;" method="post"&gt;
      Name: &lt;input type="text" name="name" pattern="[A-Za-z]" required&gt;&lt;br&gt;&lt;br&gt;
      E-mail: &lt;input type="text" name="email" pattern="/^[a-zA-Z0-9.!#$%&amp;'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required&gt;&lt;br&gt;&lt;br&gt;
      &lt;input type="submit"&gt;
  &lt;/form&gt;
  &lt;br&gt;
  &lt;?php
  print_r($_SESSION);
  echo "&lt;br&gt;Platter: " . $_SESSION["platter_quantity"] . "&lt;br&gt;";
  echo "Yogurt: " . $_SESSION["yogurt_quantity"] . "&lt;br&gt;";
  echo "Waffles: " . $_SESSION["waffles_quantity"];
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Page 3:</p>
  
  <pre><code>&lt;?php 
  session_start();
      $name = $_SESSION['name'];
      $email = $_SESSION['email'];
      $platter_quantity = $_SESSION['platter_quantity'];
      $yogurt_quantity = $_SESSION['yogurt_quantity'];
      $waffles_quantity = $_SESSION['waffles_quantity'];
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;head&gt;
      &lt;title&gt;Invoice Page&lt;/title&gt;
      &lt;link rel="stylesheet" type"text/css" href="settings.css"&gt;
  &lt;/head&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;ul&gt;
    &lt;li&gt;&lt;a href="product.php"&gt;Product&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="registration.php"&gt;Registration&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a class="active" href="invoice.php"&gt;Invoice&lt;/a&gt;&lt;/li&gt;
    &lt;li style="float:right"&gt;&lt;a href="registration.php"&gt;Login&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  
  &lt;h1&gt;Hi! Welcome &lt;?php  echo $_SESSION['name']; ?&gt;! &lt;/h1&gt;
  &lt;?php
  print_r($_SESSION);
  echo "&lt;br&gt;Platter: " . $platter_quantity . "&lt;br&gt;";
  echo "Yogurt: " . $yogurt_quantity . "&lt;br&gt;";
  echo "Waffles: " . $waffles_quantity;
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What's the best way for me to implement this using session variables without using a database?</p>
  
  <p>I tried doing this as well, but it did not seem to work:</p>
  
  <pre><code>&lt;?php 
  session_start();
      if (empty($_SESSION['name']) or empty($_SESSION['email'])) {
          $_SESSION['name'] = $POST_['name'];
          $_SESSION['email'] = $POST_['email'];
      }
      else {
          $name = $_SESSION['name'];
          $name = $_SESSION['email'];
      }
  
      if (empty($_SESSION['platter_quantity']) or empty($_SESSION['yogurt_quantity']) or ($_SESSION['waffles_quantity'])) {
          $_SESSION['platter_quantity'] = $POST_['platter_quantity'];
          $_SESSION['yogurt_quantity'] = $POST_['yogurt_quantity'];
          $_SESSION['waffles_quantity'] = $POST_['waffles_quantity'];
      }
      else {
          $platter_quantity = $_SESSION['platter_quantity'];
          $yogurt_quantity = $_SESSION['yogurt_quantity'];
          $waffles_quantity = $_SESSION['waffles_quantity'];
      }
  ?&gt;
  </code></pre>