提交表单不起作用时显示错误/成功

时间:2014-01-26 17:26:48

标签: php html forms

我的网站上有一个注册/登录系统,现在它在页面顶部显示错误和成功文本。我希望它在表单下显示它。但每当我尝试将其添加到表单下方时,它就无效了!

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

这样可行,但它会在页面顶部输出文字。

我尝试将此代码放在'form'结束标记下面:

<?php

if(isset($_POST['register']))
{

if($register->process())
echo "Successfully Signed Up!";
else
$register->show_errors();
}

?>

但是当我在表单下移动时,它会显示“无效提交”,这是由此引起的:

public function valid_token()
{
if(!isset($_SESSION['token']) || $this->token != $_SESSION['token'])
$this->errors[] = 'Invalid Submission';

return count($this->errors)? 0 : 1;
}

这是我谈论的形式

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

1 个答案:

答案 0 :(得分:1)

因此,由于valid_token()函数中的if测试失败,令牌不再在会话中(确保session_start();仍在代码的顶部),或者更可能{{1是罪魁祸首。

我有一种预感,也许你现在正在按顺序执行这行代码:

$this->token != $_SESSION['token']

如果此行在您调用$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));之前运行,那么会在您尝试匹配之前更改会话中的令牌,从而导致valid_token()函数出现问题。

如果不是这种情况,我会准确调查$register->process()设置的时间和地点,以确保在$this->token发生之前正确填充。