我正在用电子邮件验证器做一个注册系统。你典型的“使用此代码验证”类型的东西。
使这个问题如此难以诊断的是,我以类似的方式使用了许多其他会话变量,但这个变量根本不起作用。我的方法是:
/* 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; }
}