Php 提交表单后,停止重复页面上的操作

Php 提交表单后,停止重复页面上的操作,php,forms,Php,Forms,提交表单后,停止重复页面上的操作 我制作了这个“登录”表单,并在字段为空时显示错误消息和其他错误消息 我的要求是: “登录”失败后,如何阻止字段在按F5或重新加载页面时再次提交值 <?php ob_start(); session_start(); include "config/config.php"; include "includes/functions/check.php"; $userName = $passWord = ""; $userNameErr = $passWor

提交表单后,停止重复页面上的操作 我制作了这个“登录”表单,并在字段为空时显示错误消息和其他错误消息

我的要求是: “登录”失败后,如何阻止字段在按F5或重新加载页面时再次提交值

<?php
ob_start();
session_start();

include "config/config.php";
include "includes/functions/check.php";

$userName = $passWord = "";
$userNameErr = $passWordErr = $loginErr = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST['user_name'])) {
        $userNameErr = "User name is required";

    } else {
        $userName = check_input($_POST['user_name']);
        if (!preg_match("/^[a-zA-Z ]*$/", $userName)) {
            $userNameErr = "Only letters and white space allowed";
        }
    }
    if (empty($_POST['password'])) {
        $passWordErr = "Password is required";
    } else {
        $passWord = check_input($_POST['password']);
    }

    $loginUser = $db->prepare("SELECT * FROM hired_person_info WHERE user_name=? AND password=?");
    $loginUser->bind_param('ss', $userName, $passWord);
    if ($loginUser->execute()) {
        $results = $loginUser->get_result();
        if ($results->num_rows == 1) {
            $row = $results->fetch_object();
            $_SESSION['name'] = $row->full_name;
            $_SESSION['log'] = 1;
            print_r($_SESSION);
            header("Location:?pid=4");
        } elseif (!empty($_POST['user_name']) && !empty($_POST['password'])) {
            $loginErr = "Invalid Login Information";
        }
    }
}
ob_flush()
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Administration Panel</title>
    <link href="../css/adminStyle.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1 id="head" class="header_height"></h1>

<div class="contentLogin">
    <div class="login_bg">
        <div id="header">
            <p>login</p>
        </div>
        <div id="form">
            <?php
            echo $loginErr;
            ?>

            <form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

                <label for="user_name" class="text_login">User name</label>
                <input type="text" name="user_name" id="user_name" value="<?php echo $userName ?>">
                <?php echo $userNameErr; ?>
                <br/><br/>
                <label for="password" class="text_login">Password</label>
                <input type="password" name="password" id="password">
                <?php echo $passWordErr; ?>
                <br/>

                <div id="submit">
                    <input type="submit" value="Sign in" name="submit" id="submit" class="btn">
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>

行政小组
登录

如果($\u SERVER[“REQUEST\u METHOD”]==“POST”){
尝试使用提交按钮的POST名称,而不是使用
。因此条件如下:

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

}
我并不是说你使用的条件是错误的,但是当你在同一页中放置另一个表单时,可能会产生冲突

在您的操作完成后,成功或失败并不重要,只需取消设置post变量即可

unset($_POST);
要完全避免重发功能,您需要将页面重定向到同一页面一次。为此,只需在会话中保存错误消息并再次重定向到登录页面。然后检查消息的会话值是否存在,然后显示消息