PHP 清除 $_POST 数据。页面刷新时防止表单重新提交

时间:2021-02-22 01:23:07

标签: javascript php html

发布/重定向/获取 - 只是不明白((( 那个正在工作,但在第一次刷新后无法再次登录而不删除浏览器中的缓存和cookie。

localhost

这是我的简单代码:

/var/run/mysqld/mysqld.sock

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

在查看您的逻辑并重新阅读您的初始消息后,我发现了问题。这似乎是一个合乎逻辑的问题。

尝试一下我认为您正在尝试做的事情:

home.php

<?php
    session_start();
    
    // If the session is not 'loaded', send them to login.php
    if (!$_SESSION['loaded'])
    {
        header('Location: login.php');
        exit;
    }
    
    // Todo: Business logic below, etc

login.php 向某物发送帖子(比如 auth.php)

auth.php

<?php
    session_start();

    require_once('dbh.inc.php');
    require_once('functions.inc.php');

    if (!isset($_POST['submit'])) {
        exit;
    }
  
    $userMail = $_POST['email'] = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $password = $_POST['pass'] = filter_var($_POST['password'], FILTER_SANITIZE_STRING);

    // Verify the user in some way
    if( $user_is_verified ) {

        // Set this session you used
        $_SESSION['loaded'] = true;

        // Send them back to the home page, now 'loaded' in the session
        header('Location: home.php');

    } else {

        // Send them back to the login page
        // Todo: Maybe also tell them why they failed, maybe in a session variable?
        header('Location: login.php');
    }

上一个答案:根据您在评论部分的回复和可见的代码,最简单的实现方法是在登录成功时也使用 header('Location: some_success_url')

通过这种方式,刷新只会刷新 some_success_url 页面,而后退按钮只会将您带回登录页面(或者您的逻辑在用户已经通过身份验证时有效)