簡體   English   中英

提交表單不起作用時顯示錯誤/成功

[英]Show error/success when submitting form 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));

這可行,但是它在頁面頂部輸出文本。

我試圖將這段代碼放在“ 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>

因此,由於valid_token()函數中的if測試失敗,則該令牌不再存在於會話中(請確保session_start();仍位於代碼的頂部),或者更有可能是$this->token != $_SESSION['token']是元凶。

我有一種預感,也許您現在正在按順序執行以下代碼行:

$token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));

如果此行在調用$register->process()之前運行,則會話中的令牌將在嘗試匹配之前被更改,從而導致valid_token()函數出現問題。

如果不是這種情況,我將准確調查$this->token的設置時間和位置,以確保在$register->process()發生之前正確填充了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM