**Warning:** Not a duplicate, I can't find this problem.

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. 

```
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');
    }

```

However, when I go to the user profile using these cookies, `$_SESSION['userID']` is sometimes `NULL`. I can't seem to figure out why "sometimes" the `$_SESSION` goes `NULL`. 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 `loginuser.php`(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.

My problem does not appear to be answered by any of these:

[PHP SESSION data lost between page loads with WAMPserver 2.0 on localhost](https://stackoverflow.com/questions/2153465/php-session-data-lost-between-page-loads-with-wampserver-2-0-on-localhost)
-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.

[PHP Session not Saving](https://stackoverflow.com/questions/8976930/php-session-not-saving)
My `session_save_path()` is writable.

[PHP session not being saved between pages](https://stackoverflow.com/questions/6584121/php-session-not-being-saved-between-pages)
[PHP Session data not being saved](https://stackoverflow.com/questions/155920/php-session-data-not-being-saved)
I'm on localhost, not a server.

`session_start();` is on EVERY page, excluding templates that are only referenced in pages that have `session_start();` at the beginning (because if `session_start();` is in the template then it comes up with an error about how the session is already started).

**UPDATE:** 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.

Update(the relevant code from userprofile.php):

```
<?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

?>

```