代码之家  ›  专栏  ›  技术社区  ›  Tarik

注意:未定义session变量所在的索引

  •  0
  • Tarik  · 技术社区  · 15 年前

    我正在用电子邮件验证器做一个注册系统。你典型的“使用此代码验证”类型的东西。

    使这个问题如此难以诊断的是,我以类似的方式使用了许多其他会话变量,但这个变量根本不起作用。我的方法是:

    /* This is placed after the mail script and account creation within the same if 
    statement. Things get executed after it, so I know it's placed correctly. */
    
    $_SESSION['registrationComplete'] = TRUE; 
    
    // I've tried integer 1 and 'Yes' as alternatives.
    

    现在为了检查变量,我把它放在页面的顶部。

    echo $_SESSION['registrationComplete']; // To see if it's setting. This gives the
                                            // undefined index notice.
    
    if (isset($_SESSION['registrationComplete'])) {
    
    // Alternatively, I have nested another if that simply tests if it's TRUE.
    
        echo $_SESSION['registrationComplete']; // When echo'd here, it displays nothing.
    
        echo '<p>Congratulations, Foo! Go to *link to Bar*.</p>';
    
    }
    

    现在,我曾经让页面重定向到一个新的页面,但我把它拿出来测试。当页面从submit重新加载时,上面if语句中的消息出现,然后我得到一个 Notice: Undefined index: registrationComplete blah blah

    如果我回到页面,它会忽略if语句。

    我已经测试了打字错误和所有的东西,清除会话变量以防测试中的旧变量干扰,但是我没有运气。很多谷歌搜索只是显示人们抑制了这些错误,但这听起来很疯狂!不仅如此,我的站点上其他地方的会话变量的持久性也不一样。如果我做错了什么,有人能指出吗?救命啊!谢谢!

    仅供参考,我读了一些相关的问题,我也是一个初学者,所以我可能不知道如何利用某些建议没有解释。

    根据要求,添加更多代码,并添加大量注释以保持简短

    var_dump($_SESSION);
    
    // It's here to analyze that index message. I guess it's not important.
    echo $_SESSION['registrationComplete']; 
    
    if (isset($_SESSION['registrationComplete'])) { 
    
        // The golden ticket! This is what I want to appear so badly.
        echo 'Congratulations, Foo! Go to *link to Bar*.';
    
    }
    
    
    // Explanation: I don't want logged in users registering. 
    // The else statement basically executes the main chunk of code.
    
    if (isset($_SESSION['user_id'])) {
    
        echo 'You are logged in as someone already.';
    
    }
    
    else {
    
        if (isset($_POST['submitRegister'])) {
    
            // Code: Database connection and parsing variables from the form.
    
            if (!empty($email) && !empty($email2) && $email == $email2 && !empty($displayName) && !empty($password) && !empty($password2) && $password == $password2) {
    
                // Code: Query to retrieve data for comparison.
    
                if (mysqli_num_rows($registrationData) == 0) {
    
                    // Code: Generates the salt and verification code.
    
                    // Code: Password hashing and sending data to verify database.
    
                    // E-mail the verification code.
    
                    $_SESSION['registrationComplete'] = 'yes';
    
                }
    
                else {
    
                    // Some error handling is here.
                    $registerError = 'The e-mail address you entered is already in use.';
    
                }
    
            }
    
            // the elseif, elseif, and else are more error handling.
    
            elseif ($email != $email2) { $registerError = 'Your e-mails did not match'; }
    
            elseif ($password != $password2) { $registerError = 'Passwords didn\'t match.'; }
    
            else { $registerError = 'Filled out completely?'; }
    
            // If the registration was submitted, but had errors, this will print the form again.
    
            if (!isset($_SESSION['registrationComplete'])) { require_once REF_DIR . REF_REGISTERFORM; }
    
    
            // IMPORTANT! it turns out my code did not work, I forgot I had the same statement elsewhere.
            else { echo 'Congratulations, Foo! Go to *link to Bar*.'; }
        }
    
        // Creates form.
    
        else { require_once REF_DIR . REF_REGISTERFORM; }
    
    }
    
    3 回复  |  直到 15 年前
        1
  •  3
  •   George Marian    15 年前

    这可以归结为调试/故障排除的基础。

    1. 检查突出部分,确保它们是您期望的或应该的(这两者之间有细微的差别,视情况而定。)

    会话不起作用的一个典型问题是忘记使用 session_start() (靠近或在顶部)任何使用会话的页面。

    我最喜欢的PHP代码片段之一,用于调试:

    print '<pre>';
    var_dump($some_variable);
    print '</pre>';
    

    我试着用 print 用于调试和 echo 用于常规输出。一旦代码超出了输出的一些琐碎部分,就可以更容易地发现调试代码。

    与此同时, var_dump <pre></pre> 以便更容易读取输出。

        2
  •  0
  •   Pekka    15 年前

    尝试

    if (!empty($_SESSION['registrationComplete'])) {
    
        3
  •  0
  •   Karel Petranek    15 年前

    如果在打印消息后收到警告,则这不能来自回音变量,因为根据您的代码,它将在打印该消息之前抛出。您确定在if语句之外不使用$\u SESSION['registrationComplete']吗?尝试在if的右括号前添加exit或die(),查看通知是否消失。