title: PHP Session variable is not saving
body: <p>I've looked through all the problems about the session variable not saving and don't see my problem so I'm going to ask it.</p>
  
  <p>I have a form that once submitted it searches my database for that name. The initial form is on page 1.
  On page 2 I take the the variable from page 1 and save it like this</p>
  
  <pre><code>    $searchTerm = $_POST['find'];
  </code></pre>
  
  <p>which is used as the search for the database and it works perfectly. Under that I have my sql statement then I have placed this</p>
  
  <pre><code>    //initialize the session
      if (!isset($_SESSION)) {
        session_start();
      }
      $_SESSION['searchTerm'] = $searchTerm;
  </code></pre>
  
  <p>I have then tested the $_SESSION['searchTerm'] on page 2 to make sure that it is saving properly, and it does. My issue comes in when I try to go to a 3rd page that is a confirm page for the form from page 2. I have the session start clause at the top of the 3rd page and I even inserted </p>
  
  <pre><code>ini_set('display_errors',1); 
  error_reporting(E_ALL); 
  </code></pre>
  
  <p>to check for errors, no errors were displayed. So my next step was to test the session itself to see if it was refreshing the session id. I found a script on this site www.webassist.com/forums/posts.php?id=5735 to test whether the server could possibly be causing the issue. The script works fine no problems, showing that the server is not the problem. However when I upload my 2nd and 3rd page to the server and put in </p>
  
  <pre><code>    &lt;?php echo session_id(); ?&gt;
  </code></pre>
  
  <p>the numbers from page 2 and page 3 are completely different therefore making my variable null. I did further research and thought it might be because there was a session_destroy or session_unset but I didn't put one of these on either page. When I tried this on my local machine I got the same session id but still the session variable I set was blank. </p>
  
  <p>Does anyone have any other ideas on how or why this would happen?</p>
  
  <p><em><strong></em>**<em>*</em>**<em>*</em>****</strong><em>edit <strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong></p>
  
  <p>page 1 has this form</p>
  
  <pre><code>                &lt;form name="search" method="post" action="page2.php"&gt;
                      &lt;div&gt;
                      &lt;!-- Search--&gt;
                      &lt;label&gt;Search by Last Name:&lt;/label&gt;
                          &lt;div&gt;
                              &lt;table width="100%"&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;&lt;input type="text" id="" name="find" placeholder=""&gt;&lt;/td&gt;
                                      &lt;td&gt;&lt;button id="submit" type="submit"&gt;&lt;span&gt;Search&lt;/span&gt;&lt;/button&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/form&gt; 
  </code></pre>
  
  <p>page 2</p>
  
  <pre><code>    $searchTerm = $_POST['find'];
  
  
      if(!empty($searchTerm ) &amp;&amp; ctype_alpha($searchTerm ))
      {
      $whereclause = "WHERE mytable.lastName = '".$searchTerm ."'";
      }
      else {
      $whereclause = "";
      }
  
      // sql query is here this one searches it is long and just used the $whereclause above.
  
      // second sql query that is only submitted if button from the form below is hit is here.
  
      $insertGoTo = "confirmPage3.php";
        if (isset($_SERVER['QUERY_STRING'])) {
          $insertGoTo .= (strpos($insertGoTo, '?')) ? "&amp;" : "?";
          $insertGoTo .= $_SERVER['QUERY_STRING'] ;
  
        }
        header(sprintf("Location: %s", $insertGoTo));
  
      //initialize the session
      if (session_id() == '') {
        session_start();
      }
      $_SESSION['searchTerm'] = $searchTerm;
  
  
      // then the form is listed below that with the rest of the html 
  </code></pre>
  
  <p>page 3</p>
  
  <pre><code>    &lt;?php if(session_id() == '') session_start(); ?&gt; 
  
      if(!empty($_SESSION['searchTerm']) &amp;&amp; ctype_alpha($_SESSION['searchTerm']))
      {
      $whereclause = "WHERE myTable.lastName = '".$_SESSION['searchTerm'] ."'";
      }
      else {
      $whereclause = "";
      }
  
      // More sql statement here for this one it is only selecting not updating or changing anything.
  
      // Table below this that is used to hold the items retrieved from the database.
  </code></pre>
  
  <p><em><strong></em>**<em>*</em>**<em>*</em>**<em>*</em>**</strong><em>edit 2 and my fix</em><strong><em>*</em>**<em>*</em>**<em>*</em>**<em>*</em></strong></p>
  
  <p>so after some back and for with a few of you, Aaron Gong suggested I start with a blank page and go from there. I did this and have finally diagnosed the issue. It was something that I hadn't thought of that I very well should have. I don't like using dreamweaver and now I remember why. When I originally created the page 2 I used dreamweavers insert code to insert my sql statement to do the updating. Well it placed in the code these lines of code.</p>
  
  <pre><code>    $editFormAction = $_SERVER['PHP_SELF'];
      if (isset($_SERVER['QUERY_STRING'])) {
        $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
      }
  </code></pre>
  
  <p>This will completely mess with you if you are trying to send the user to another page to confirm their entries. I commented it out and set the $editFormAction to my page and used that in the header string and viola that immediately fixed my issue. What I hadn't realized is when it was refreshing the page that it was emptying my $_POST variable and found that error finally through the empty pages suggest by Aaron.</p>
  
  <p>Hope this helps someone else and I will never trust the code from dreamweaver again. I learned to code by hand and should know better but I was in a hurry and thought oh it won't harm it.</p>
  
  <p>Thanks again to all your suggestions.</p>