PHP:注意:定义了会话变量的未定义索引 [英] PHP: Notice: Undefined index where the session variable is defined

查看:228
本文介绍了PHP:注意:定义了会话变量的未定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用电子邮件验证器注册系统。您的典型使用此代码验证的类型。



我想要一个会话变量被存储,这样当人们在注册页面上完成帐户注册时,不知何故在意外回到页面,它提醒他们,他们需要在使用前激活他们的帐户。



这个问题难以诊断是我使用过的许多其他会话变量以类似的方式,但这一个是不工作的。这是我的方法:

  / *这是在邮件脚本和帐户创建之后放在同样的
语句中。事情被执行之后,所以我知道它被正确放置。 * /

$ _SESSION ['registrationComplete'] = TRUE;

//我已经尝试了整数1和'是'作为替代。

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

  echo $ _SESSION ['registrationComplete']; //查看是否设置。这给出
//未定义的索引通知。

if(isset($ _ SESSION ['registrationComplete'])){

//或者,如果只是简单地测试它是否为真,我嵌套另一个。

echo $ _SESSION ['registrationComplete']; //当回显在这里时,它什么都不显示。

echo'< p>祝贺,Foo!转到*链接到Bar *。< / p>';

}

现在,我曾经将页面重定向到一个新的页面,但我拿出来测试它。当页面从提交中重新加载时,我的消息在上面的if语句中出现,然后我得到一个注意:未定义的索引:registrationComplete blah blah 从回显的会话var! / p>

然后,如果我回到页面,它将一起忽略if语句。



我已经测试对于打字错误和一切,清除会话变量,以防旧的测试被干扰,但我没有运气。很多谷歌只是显示人们抑制这些错误,但这听起来很疯狂!不仅如此,但是我的网站上其他地方的会话变量的持久性并不如此。有人可以指出,如果我正在做一些公然错误的事情?帮帮我!谢谢!



FYI,我读了几个相关问题,我也是初学者,所以我可能不知道如何利用某些建议,无需解释。



根据要求,更多代码,大量注释以保持简洁

  var_dump($ _ SESSION); 

//这里是分析索引消息。我猜这不重要
echo $ _SESSION ['registrationComplete'];

if(isset($ _ SESSION ['registrationComplete'])){

//金票!这就是我想出现的那么糟糕。
echo'恭喜,Foo!转到*链接到酒吧*。

}


//说明:我不想登录用户注册。
// else语句基本上执行主要的代码块。

if(isset($ _ SESSION ['user_id'])){

echo'您已经以某人身份登录了。

}

else {

if(isset($ _ POST ['submitRegister'])){

/ /代码:数据库连接并从表单中解析变量。

如果(!empty($ email)&&!empty($ email2)&&$ $ === $ email2&!empty($ displayName)& !空($ password)&&!空($ password2)&& $ password == $ password2){

//代码:查询数据进行比较。

if(mysqli_num_rows($ registrationData)== 0){

//代码:生成盐和验证码。

//代码:密码散列和发送数据来验证数据库。

//通过电子邮件发送验证码。

$ _SESSION ['registrationComplete'] ='yes';

}

else {

//有些错误处理在这里。
$ registerError ='您输入的电子邮件地址已被使用。

}

}

// elseif,elseif,还有更多的错误处理。

elseif($ email!= $ email2){$ registerError ='您的电子邮件不匹配'; }

elseif($ password!= $ password2){$ registerError ='密码没有匹配'。 }

else {$ registerError ='完全填满' }

//如果注册已提交但有错误,则会再次打印该表单。

if(!isset($ _ SESSION ['registrationComplete'])){require_once REF_DIR。 REF_REGISTERFORM; }


//重要!原来我的代码没有工作,我忘了我在其他地方有同样的声明。
else {echo'恭喜,Foo!转到*链接到酒吧*。 }
}

//创建表单。

else {require_once REF_DIR。 REF_REGISTERFORM; }

}


解决方案

了解调试/故障排除的基础知识。


  1. 尽可能多了解技术/库/函数/重新尝试使用。

  2. 检查显着位,并确保它们是您期望的或应该是什么。 (根据情况,这两者之间有轻微的差异。)

  3. 如果这不能解决问题,请退后一步,确保您了解情况。这可能意味着简化的事情,以便您只处理手头的问题,即创建一个单独的,更简单的测试用例,暴露相同的问题。或者,它可能只是意味着你停止编码,并通过你的代码的流程来确保它真的在做你所想的。

会话无效的典型问题是忘记在使用会话的任何页面中使用 session_start()(靠近或顶部)。



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

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

我尝试使用打印进行调试 echo 用于常规输出。一旦超出了一些微不足道的输出,就可以更容易地发现调试代码。



同时, var_dump 将打印一些有关变量的信息,就像它的类型和大小一样。重要的是将它包装在< pre>< / pre> 中,以便更容易阅读输出。


I am making a registration system with an e-mail verifier. Your typical "use this code to verify" type of thing.

I want a session variable to be stored, so that when people complete their account registration on the registration page and somehow navigate back to the page on accident, it reminds them that they need to activate their account before use.

What makes this problem so hard to diagnose is that I have used many other session variables in similar ways, but this one is not working at all. Here's my approach:

/* 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.

Now to check for the variable, I placed this at the top of the page.

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>';

}

Now, I used to have the page redirect to a new page, but I took that out to test it. When the page reloads from submit, my message in the if statement above appears and then I get an Notice: Undefined index: registrationComplete blah blah from the echoing of the session var!

Then if I ever go back to the page, it ignores the if statement all together.

I have tested for typos and everything, clearing session variables in case old ones from testing were interfering, but I am having no luck. A lot of Googling just shows people suppressing these errors, but that sounds insane! Not only that, but I am not getting the same persistence of session variables elsewhere on my site. Can someone point out if I'm doing something blatantly wrong? Help! Thanks!

FYI, I read several related questions and I am also a beginner, so I may not know how to utilize certain advice without explanation.

As requested, more code, heavily annotated to keep it brief

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; }

}

解决方案

This came down to the basics of debugging/troubleshooting.

  1. Understand as much as you can about the technique/library/function/whatever that you're trying to use.
  2. Inspect the salient bits and make sure that they are what you expect or what they should be. (There's a slight difference between those two, depending on the situation.)
  3. If that doesn't bring you towards a solution, step back and make sure you're understanding the situation. This may mean simplifying things so that you're only dealing with the issue at hand, i.e. create a separate, simpler test case which exposes the same problem. Or, it may simply mean that you stop coding and work through the flow of your code to make sure it is really doing what you think it is doing.

A typical issue with sessions not working is forgetting to use session_start() (near or at the top) of any page which uses sessions.

One of my favorite snippets of PHP code, for debugging:

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

I try to use print for debugging and echo for regular output. It makes it easier to spot debugging code, once it's goes beyond a few trivial bits of output.

Meanwhile, var_dump will print a bit more info about the variable, like it's type and size. It's important to wrap it in <pre></pre> so that it's easier to read the output.

这篇关于PHP:注意:定义了会话变量的未定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆