简体   繁体   中英

Session variable not staying set

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 -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 My session_save_path() is writable.

PHP session not being saved between pages 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

?>

This bit of code basically does nothing:

$cookieresult = mysqli_query($connection, 'SELECT COUNT(`ID`) AS count FROM users WHERE username="$_COOKIE["usercookie"]" AND `active`="1"');

As $cookieresult well only loosely equal 0 (it will actually be false ) when the query actually fails. I am guessing the what you really need to do is to actually read the data out of the result resource handle that gets set to $cookieresult to determine your value for count(ID) .

Here is the answer in code, if anyone needs it in the future, or was interested. I'm not sure what all of the code was doing in the original, looking back, I probably should study more SQL syntax. Thanks for the help, Mike.

if(isset($_COOKIE["usercookie"])) {
    if(isset($_COOKIE["IPcookie"])) {
        echo('Cookie detected, processing, wait a second.');
        if($_COOKIE["IPcookie"] == $_SERVER['REMOTE_ADDR']) {
            $cookieusername = $_COOKIE["usercookie"];
            $cookieresult = mysqli_query($connection, "SELECT * FROM users WHERE username='$cookieusername' AND active=1");

            if(!$cookieresult or mysqli_num_rows($cookieresult) == 0) {
                echo('We really don\'t believe your cookie is real, sorry.');           
            }
            else {
                $loginquery = mysqli_query($connection, "SELECT * FROM users WHERE username='$cookieusername'");
                $row5 = mysqli_fetch_assoc($loginquery);
                $dbIDlogin = $row5['ID']; 
                $_SESSION['userID'] = $dbIDlogin;
                session_write_close();
                header('Location: /userprofile.php');
            }   
        }   
        else {
            echo('We don\'t currently believe your cookie is real, sorry.');

        }
    }
    else {
        echo('We don\'t believe your cookie is real, sorry.');

    }
}

Check if enter into if-statement or not. If not please check if "output_buffering" from php.ini set "ON"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM