title: Session variable not staying set
body: <p><strong>Warning:</strong> Not a duplicate, I can't find this problem.</p>
  
  <p>I'm testing and building my website on localhost. I have the following code on my login page, sending the user to their profile page if they have a user ID cookie, and their IP is the same as their cookies says it should be. </p>
  
  <pre><code>if($_COOKIE["IPcookie"] == $_SERVER['REMOTE_ADDR']) {
      $cookieresult = mysqli_query($connection, 'SELECT COUNT(`ID`) AS count FROM users WHERE username="$_COOKIE["usercookie"]" AND `active`="1"');
      if($cookieresult == 0) {
          $loginquery = mysqli_query($connection, "SELECT * FROM users WHERE username=" .$_COOKIE['usercookie']);
          $row5 = mysqli_fetch_assoc($loginquery);
          $dbIDlogin = $row5['ID']; 
          $_SESSION['userID'] = $dbIDlogin;
          header('Location: /userprofile.php');
      }
  </code></pre>
  
  <p>However, when I go to the user profile using these cookies, <code>$_SESSION['userID']</code> is sometimes <code>NULL</code>. I can't seem to figure out why "sometimes" the <code>$_SESSION</code> goes <code>NULL</code>. I can't seem to find any pattern in it. I login one user, click to all the links on their profile page that go to different areas of the website and then go back to <code>loginuser.php</code>(the file shown above), and it works with one user, but doesn't with another. It seems to be completely random, which confuses me. I can't think of any reason it would change inbetween users. I'll continue to test this, and see if I can find a pattern, but with several hours put in already, it appears completely random.</p>
  
  <p>My problem does not appear to be answered by any of these:</p>
  
  <p><a href="https://stackoverflow.com/questions/2153465/php-session-data-lost-between-page-loads-with-wampserver-2-0-on-localhost">PHP SESSION data lost between page loads with WAMPserver 2.0 on localhost</a>
  -I checked my php.ini, it has those lines of code (though a different, but functioning tmp file). The example code in the selected answer testing the $_SESSION variable works like a charm. My code, doesn't.</p>
  
  <p><a href="https://stackoverflow.com/questions/8976930/php-session-not-saving">PHP Session not Saving</a>
  My <code>session_save_path()</code> is writable.</p>
  
  <p><a href="https://stackoverflow.com/questions/6584121/php-session-not-being-saved-between-pages">PHP session not being saved between pages</a>
  <a href="https://stackoverflow.com/questions/155920/php-session-data-not-being-saved">PHP Session data not being saved</a>
  I'm on localhost, not a server.</p>
  
  <p><code>session_start();</code> is on EVERY page, excluding templates that are only referenced in pages that have <code>session_start();</code> at the beginning (because if <code>session_start();</code> is in the template then it comes up with an error about how the session is already started).</p>
  
  <p><strong>UPDATE:</strong> I may have a pattern. Users with usernames including letters do not have their information saved. All of these users also have longer usernames, as their usernames are words like "dummy" and "eport" as opposed to "1" and "9." It works for 1 and 9, but not for dummy and eport. Now, this makes no sense, as they are all identified by their ID's which are always numbers, but this does appear to be a pattern.</p>
  
  <p>Update(the relevant code from userprofile.php):</p>
  
  <pre><code>&lt;?php
  
  $userID = $_SESSION['userID'];
  var_dump($_SESSION['userID']);
  
  
  //get.
  $totalquery = mysqli_query($connection, "SELECT * FROM users WHERE ID='$userID'");
  $arrayquery = mysqli_fetch_assoc($totalquery);
  $username = $arrayquery['username'];
  
  //Firstname
  $firstname = $arrayquery['firstname'];
  echo 'Welcome ' .  $firstname;
  //Lastname
  
  ?&gt;
  </code></pre>