Php 如果密码/电子邮件验证失败,如何将用户重定向到同一URL?

Php 如果密码/电子邮件验证失败,如何将用户重定向到同一URL?,php,validation,redirect,Php,Validation,Redirect,我有一个密码重置脚本,有电子邮件和密码验证。如果用户未能满足验证要求,我如何使脚本将用户重定向到相同的URL(即“…”)?当前,如果验证测试失败,用户将重定向到URL:。令牌现在消失,密码重置页面变得无用 以下是我的PHP代码: <?php ob_start(); session_start(); include 'connect.php'; // Was the form submitted? if (isset($_POST['btn-reset'])) {

我有一个密码重置脚本,有电子邮件和密码验证。如果用户未能满足验证要求,我如何使脚本将用户重定向到相同的URL(即“…”)?当前,如果验证测试失败,用户将重定向到URL:。令牌现在消失,密码重置页面变得无用

以下是我的PHP代码:

<?php
  ob_start();
  session_start();
  include 'connect.php';

// Was the form submitted?
if (isset($_POST['btn-reset']))
      {
// Gather the post data
$email = trim($_POST['email']);
$email = strip_tags($email);

$pass = trim($_POST['pass']);
$pass = strip_tags($pass);

$cpass = trim($_POST['cpass']);
$cpass = strip_tags($cpass);


//basic email validation
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
    $error = true;
    $emailError = "Please enter valid email address.";

}

// password validation
if (empty($pass)){
    $error = true;
    $passError = "Please enter password.";
} else if(strlen($pass) < 6) {
    $error = true;
    $passError = "Password must have at least 6 characters.";
} else if($pass != $cpass) {
    $error = true;
    $passError = "Passwords do not match.";
}
// if there's no error, continue to process
if( !$error ) {

$token = $_POST ['token'];

// Retrieve token from database
$stmt = $conn->prepare('SELECT token FROM token WHERE userEmail=? and NOW() < expire_date');
$stmt->bind_param('s', $email);
$stmt->execute();

$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    $resetKey = $row['token'];
}
// Does the new reset key match the old one?
if ($resetKey == $token && isset($token))
{
    if ($pass == $cpass)
    {
        //hash and secure the password
        $password = password_hash($pass, PASSWORD_DEFAULT);

        // Update the user's password
            $stmt = $conn->prepare('UPDATE user SET userPass = ? WHERE userEmail = ?');
            $stmt->bind_param('ss', $password, $email);
            $stmt->execute();
            $conn = null;
        $sucMSG = "Your password has been successfully reset.";
        unset($email);
        unset($pass);
        unset($cpass);
        unset($token);
        unset($resetKey);
    }
    else
        $matchError = "Your password's do not match.";
}
else
    $keyError = "Your password reset key is invalid.";
 }
}
?>

print\r($\u SERVER)
并找到您需要的内容。您可能应该使用ajax发布表单。@HSharma使用ajax发布表单会消除此问题吗?是的,这就是人们在遇到此类问题时首选ajax的原因。
print\r($\u SERVER)
并找到您需要的内容。您可能应该使用ajax发布表单。@hs使用ajax发布表单会消除此问题吗?是的,这就是人们在遇到此类问题时首选ajax的原因。