我正在建立一个带有电子邮件验证程序的注册系统。您典型的“使用此代码验证”类型的东西。
我希望存储一个会话变量,以便当人们在注册页面上完成他们的帐户注册并以某种方式导航回事故页面时,它会提醒他们需要在使用前激活他们的帐户。
这个问题如此难以诊断的原因是我以类似的方式使用了许多其他会话变量,但这个变量根本不起作用。这是我的方法:
/* 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>';
}
现在,我曾经将页面重定向到新页面,但我把它用来测试它。当页面从提交重新加载时,我的消息在上面的if语句中出现,然后我从会话var的回显中得到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; }
}
答案 0 :(得分:3)
这归结为调试/故障排除的基础知识。
会话无效的典型问题是忘记使用任何使用会话的网页session_start()
(靠近或位于顶部)。
我最喜欢的PHP代码片段之一,用于调试:
print '<pre>';
var_dump($some_variable);
print '</pre>';
我尝试使用print
进行调试,使用echo
进行常规输出。一旦它超出了几个微不足道的输出位,就可以更容易地发现调试代码。
与此同时,var_dump
会打印更多关于变量的信息,比如它的类型和大小。将它包装在<pre></pre>
中非常重要,这样可以更容易地读取输出。
答案 1 :(得分:0)
尝试
if (!empty($_SESSION['registrationComplete'])) {
答案 2 :(得分:0)
如果在打印消息后收到警告,则这不能来自变量回显,因为根据您的代码,它会在打印该消息之前抛出。你确定你没有在if语句之外使用$ _SESSION ['registrationComplete']吗?尝试在if的结束括号之前添加exit或die(),看看通知是否消失。