简体   繁体   English

如果尚未设置会话变量,则无法设置$ _SESSION变量

[英]Cannot set $_SESSION variable when a session variable hasn't already been set

I'm a bit stumped as for an answer to this one. 对于这个答案,我有些困惑。

I have a registration form, and if there is a problem with a form input when it gets checked I'm saving a message to a session variable then checking to see if that session variable has anything it in and if it does, displaying the output at the top of my page I call session_start(); 我有一个注册表单,如果在检查表单输入时出现问题,我会将消息保存到会话变量中,然后检查该会话变量中是否包含任何内容,如果有,则显示输出在页面顶部,我调用session_start(); and when setting the message I use something like $_SESSION['msg'] = "Fill out all the information"; 当设置消息时,我使用类似$_SESSION['msg'] = "Fill out all the information";

The problem I have however is that this only works when a user is logged in (IE a session has already been started) and when there is no session the $_SESSION['msg'] variable doesn't get set. 但是,我的问题是,这仅在用户登录时有效(即,一个会话已经启动),并且在没有会话时,不会设置$ _SESSION ['msg']变量。

To check the error message I use this code. 要检查错误消息,我使用此代码。

        if(isset($_SESSION['msg'])){
            echo $_SESSION['msg'];
            unset($_SESSION['msg']);
        }

As you can see by the code at the top of the page, there is very little that sits between these. 如您在页面顶部的代码中所见,它们之间几乎没有什么关系。 But there is a check done to see if the user has a valid session but all this does is either return true or false depending on how it evaluates, I've check it and it doesn't seems to be causing the problem as when I take it out, the issue still persists. 但是需要进行检查以查看用户是否具有有效的会话,但是根据评估的结果,返回的结果是否为true或false,我已经对其进行了检查,但似乎并没有引起问题。取出来,问题仍然存在。

Can anyone shed some light on this problem? 任何人都可以阐明这个问题吗?

session_start();
include_once("".$_SERVER['DOCUMENT_ROOT']."/includes/Krumo/class.krumo.php");
include_once("".$_SERVER['DOCUMENT_ROOT']."/auth/class_loader.php");
    $sessCheck = new userFunc;
    if($sessCheck->validSess('bool')){
        $sess = true;
    }
    else{ $sess = false;}


if(isset($_POST['email'])){
    if($_POST['email'] != NULL && $_POST['username'] != NULL && $_POST['pass'] != NULL && $_POST['rpass'] != NULL){
        $e = preg_replace('#[^a-z0-9_@.-]#i', '', $_POST['email']);
        if (!filter_var($e, FILTER_VALIDATE_EMAIL)) {
            $_SESSION['msg'] = "Please enter a valid email";
            return false;
        }
        else{

            $check = new userFunc;
            if($check->userExists($_POST['email'],$_POST['username'])){
                $_SESSION['msg'] = "An account with that email or username already exists";
                return false;
            }
            else
            {
                if(!$check->pswdMatch($_POST['pass'],$_POST['rpass'])){
                    $_SESSION['msg'] = "Passwords do not match";
                    return false;
                }
                $register = new register;
                $user = $register->startReg($_POST['username'],$_POST['email'],$_POST['pass']);
            }
        }
    }else
    {
        $_SESSION['msg'] = "Fill out all the information";
        return false;
    }
}

I have found what the issue was. 我发现了问题所在。 Further down on my page I was building an account bar that sits in the top right of the page, this account bad uses the same $_SESSION['msg'] for the login form that sits in that bar at the top, and because there was a message set inside it, it was echoing out the message, then unsetting it, then reloading the page. 在我页面的下方,我正在建立一个位于页面右上方的帐户栏,该帐户错误地使用了位于顶部该栏中的登录表单相同的$_SESSION['msg']是在其中设置的一条消息,它是回显该消息,然后取消设置它,然后重新加载页面。

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

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