title: PHP session, why is session_start() required multiple times?
body: <p>I am writing a web application that saves POSTed data to a session in one page, then redirects to another page to utilize the created session information.  This was after I read that the proper way to process data and display data is to separate them into two different scripts so as not to run into a redundant $_POST data issue.  That is, not to keep $_POSTing the same data to the server every page refresh.</p>
  
  <p>I have a view page, <code>index.php</code>, and a data processing page, <code>setDate.php</code>.  When viewing <code>index.php</code>, the user can choose to set <code>$_POST['month']</code> and <code>$_POST['year']</code> variables via an input form, and submit them to setDate to assign <code>$_SESSION['desMonth']</code> and <code>$_SESSION['desYear']</code> respectively.  </p>
  
  <p>It wasn't until I added a second (IMO redundant) <code>session_start();</code> declaration on <code>setDate.php</code> that the code started to work the way I wanted to.  Without it, it was as if <code>index.php</code> was ignoring <code>setDate.php</code>'s <code>$_SESSION[*]</code> modifications completely.</p>
  
  <p>Why do I have to define this redundant <code>session_start();</code> if I already started the session (and received the PHPSESSID cookie) on the initial <code>index.php</code> where the <code>$_SESSION[*]</code> data is being used?</p>
  
  <p>Here are some working code snippets:</p>
  
  <p><code>setDate.php</code></p>
  
  <pre><code>&lt;?php
  require_once 'jan.php';
  session_start();
  
  //get the requested month and years to view (iterative).
  if(isset($_POST['nextMonth']) &amp;&amp; filter_var($_POST['nextMonth'], FILTER_SANITIZE_NUMBER_INT)) { //this filter only allows +- and 0-9
      $_SESSION['desMonth'] += sanitizeInput($_POST['nextMonth']);
      if($_SESSION['desMonth'] &gt; 12) {
          $_SESSION['desMonth'] = $_SESSION['desMonth']-12;
          $_SESSION['desYear'] += 1;
      }
      else if($_SESSION['desMonth'] &lt; 1) {
          $_SESSION['desMonth'] = 12;
          $_SESSION['desYear'] -= 1;
      }
  }
  
  //get the explicit month and years to view.
  if(isset($_POST['month']) &amp;&amp; filter_var($_POST['month'], FILTER_SANITIZE_NUMBER_INT)) {
      $_SESSION['desMonth'] = sanitizeInput($_POST['month']);
      echo "set month";
  }
  if(isset($_POST['year']) &amp;&amp; filter_var($_POST['year'], FILTER_SANITIZE_NUMBER_INT)) {
      $_SESSION['desYear'] = sanitizeInput($_POST['year']);
      echo "set year";
  }
  
  echo $_SESSION['desMonth'];
  echo $_SESSION['desYear'];
  header("Location: /");
  die();
  
  ?&gt;
  </code></pre>
  
  <p>Truncated <code>index.php</code></p>
  
  <pre><code>&lt;?php 
  session_start();
  require_once 'cellUpdater.php';
  
  $timeForDateUse = mktime(1,1,10,$_SESSION['desMonth'],1,$_SESSION['desYear']); //this line is used for various formatting below.
  ...
  </code></pre>
  
  <p>Without the <code>session_start();</code> declaration in <code>setDate.php</code> the <code>$_SESSION[*]</code> data will not be preserved.  Why?</p>
  
  <p>EDIT: Question answered, editing for imaginary internet points</p>