简体   繁体   English

会话变量未设置

[英]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. 我的登录页面上有以下代码,如果用户具有用户ID cookie,并且用户的IP与cookie所显示的IP地址相同,则将用户发送到其个人资料页面。

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 . 但是,当我使用这些cookie进入用户个人资料时, $_SESSION['userID']有时为NULL I can't seem to figure out why "sometimes" the $_SESSION goes NULL . 我似乎无法弄清楚为什么有时$_SESSION变为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. 我登录一个用户,单击他们的个人资料页面上所有指向网站不同区域的链接,然后返回到loginuser.php (上面显示的文件),它loginuser.php一个用户有效,而对另一个用户无效。 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). 在localhost上使用WAMPserver 2.0进行页面加载之间的PHP SESSION数据丢失 -我检查了php.ini,它具有这些代码行(尽管是一个不同但功能正常的tmp文件)。 The example code in the selected answer testing the $_SESSION variable works like a charm. 所选答案中测试$ _SESSION变量的示例代码的工作原理像一个超级按钮。 My code, doesn't. 我的代码不是。

PHP Session not Saving My session_save_path() is writable. PHP会话未保存我的session_save_path()可写。

PHP session not being saved between pages PHP Session data not being saved I'm on localhost, not a server. 页面之间未保存PHP会话PHP会话未保存 数据我在localhost而不是服务器上。

session_start(); is on EVERY page, excluding templates that are only referenced in pages that have session_start(); 在每个页面上,不包括仅在具有session_start();页面中引用的模板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). 在开始时(因为如果session_start();在模板中,则它会提示有关如何启动会话的错误)。

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." 所有这些用户还具有更长的用户名,因为他们的用户名是“ dummy”和“ eport”之类的单词,而不是“ 1”和“ 9”。 It works for 1 and 9, but not for dummy and eport. 它适用于1和9,但不适用于虚拟人和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. 现在,这没有任何意义,因为它们都是由始终为数字的ID标识的,但这似乎是一种模式。

Update(the relevant code from userprofile.php): 更新(来自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. 由于$ cookieresult恰好在查询实际失败时大致等于0 (实际上为false )。 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) . 我猜测您真正需要做的是从结果资源句柄中实际读取数据,该结果资源句柄已设置为$cookieresult以确定您的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. 我不确定所有代码在原始代码中的作用,回想起来,我可能应该研究更多的SQL语法。 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语句。 If not please check if "output_buffering" from php.ini set "ON" 如果不是,请检查php.ini中的“ output_buffering”是否设置为“ ON”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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