[英]Show error/success when submitting form not working
I got a registration/login system on my site, and as it is right now it displays errors and success text at top of the page. 我的网站上有一个注册/登录系统,目前它在页面顶部显示错误和成功文本。 I want it to display it under the form. 我希望它在表格下显示。 But whenever I try to add it below the form, it's not working! 但是,每当我尝试将其添加到表单下方时,它都无法正常工作!
session_start();
include_once('class.register.php');
$register = new Register();
if(isset($_POST['register']))
{
if($register->process())
echo "Successfully Signed Up!";
else
$register->show_errors();
}
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
This works, but it outputs the text on top of the page. 这可行,但是它在页面顶部输出文本。
I tried to put this code below the 'form' end tag: 我试图将这段代码放在“ form”结束标签下面:
<?php
if(isset($_POST['register']))
{
if($register->process())
echo "Successfully Signed Up!";
else
$register->show_errors();
}
?>
But when I move it down under the form then it says 'Invalid Submission', which is caused by this: 但是,当我将其移到表格下方时,它会显示“无效提交”,这是由于以下原因引起的:
public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Invalid Submission';
return count($this->errors)? 0 : 1;
}
THIS IS THE FORM I AM TALKING ABOUT 这是我要谈的表格
<form role="form" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label for="firstname">Firstname</label>
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="Firstname">
</div>
<div class="col-sm-6">
<label for="surname">Surname</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname">
</div>
</div>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="ruser" name="ruser" placeholder="Username">
</div>
<div class="form-group">
<label for="email2">Email address</label>
<input type="email" class="form-control" id="remail" name="remail" placeholder="Enter email">
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label for="password2">Password</label>
<input type="password" class="form-control" id="rpass" name="rpass" placeholder="Password">
</div>
<div class="col-sm-6">
<label for="password2">Repeat password</label>
<input type="password" class="form-control" id="rpass2" name="rpass2" placeholder="Password">
</div>
</div>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
</label>
</div>
<input type="hidden" name="token" value="<?php echo $token;?>"/>
<button type="submit" name="register" class="btn btn-block btn-color btn-xxl">Create an account</button>
</form>
So since the if test in the valid_token() function is failing either the token isn't in the session anymore (make sure session_start();
is still at the top of your code), or more likely $this->token != $_SESSION['token']
is the culprit. 因此,由于valid_token()函数中的if测试失败,则该令牌不再存在于会话中(请确保session_start();
仍位于代码的顶部),或者更有可能是$this->token != $_SESSION['token']
是元凶。
I have a hunch that maybe you are doing this line of code out of order now: 我有一种预感,也许您现在正在按顺序执行以下代码行:
$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
If this line runs before your call to $register->process()
then the token inside the session will be be changed before you try to match it, causing the problem in the valid_token() function. 如果此行在调用$register->process()
之前运行,则会话中的令牌将在尝试匹配之前被更改,从而导致valid_token()函数出现问题。
If this is not the case, I would investigate exactly when and where $this->token
gets set to make sure it is populated correctly before $register->process()
happens. 如果不是这种情况,我将准确调查$this->token
的设置时间和位置,以确保在$register->process()
发生之前正确填充了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.