Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 提交刷新问题,如何在不使用header函数的情况下获取_Php_Refresh - Fatal编程技术网

Php 提交刷新问题,如何在不使用header函数的情况下获取

Php 提交刷新问题,如何在不使用header函数的情况下获取,php,refresh,Php,Refresh,我的问题是: 我无法阻止页面刷新时提交。我正在为一个游戏创建一个小应用程序,但我的问题是,每次我刷新我的页面时,我的数据表都会提交到主页,所以我的记录会变得疯狂 这是我的密码: index.php <?php $_SESSION = []; $title = 'Index'; ?> <div class="container" id="signin"> <div> <div> <form action

我的问题是:

我无法阻止页面刷新时提交。我正在为一个游戏创建一个小应用程序,但我的问题是,每次我刷新我的页面时,我的数据表都会提交到主页,所以我的记录会变得疯狂

这是我的密码:

index.php

<?php

$_SESSION = [];
$title = 'Index';

?>

<div class="container" id="signin">
   <div>
      <div>
         <form action="untitled.php" method="POST" data-parsley-validate="">
            <label>Nome :</label><input type="text" name="username" class="box" required data-parsley-error-message="Inserisci il tuo username">
            <label>Password :</label><input type="password" name="password" class="box" required data-parsley-error-message="Inserisci la tua password">
            <?php
            if(isset($_GET["errors"])) {
               foreach ($_GET["errors"] as $value) {
                  echo '<p>'.$value.'</p>';
               }
            }
            ?>
            <input type="submit" value="Entra!" name="submit">
         </form>
      </div>
   </div>
</div>

问题是,由于某种原因,刷新时该变量的计算结果将始终为true,我希望避免这种情况。有什么想法吗?

我不明白您对
header()
-函数有什么问题?这应该在最后完成,那么为什么在此之后还需要
$\u POST
-数据呢?向php文件发出请求,对发布的数据执行需要执行的操作,完成后重定向客户端。如果可能,更好的解决方案是使用Ajax。这样页面就不会重新加载。如果要保留post数据以重新填充表单,可以将发布的数据放入会话变量中,然后使用该变量重新填充表单并在之后清除该会话变量(如flash会话),我会选择使用Ajax,虽然我知道,我只是在寻找一些简单的方法来防止表单在重新加载时重新提交,但是我还没有找到任何东西..它的实现方式是使用header(),所以这可能就是为什么很难找到替代方法的原因并让页面实际完成重定向回
index.php
,那么您就不会有问题了。只有在使用untitle.php时,才会看到刷新有问题
<?php

session_start();

if(isset($_POST['submit'])) {
  var_dump(isset($_POST['submit']));
  die();
  $myusername = $_POST['username'];
  $mypassword = $_POST['password'];

  $sql = $pdo->prepare('SELECT * FROM user WHERE username = ?');
  $sql->bindValue(1, $myusername, PDO::PARAM_STR);
  $sql->execute();

  $rows = $sql->fetchAll(PDO::FETCH_NUM);

  if (empty($rows)) {

    $errors = array(
      "username" => "Non ci sono corrispondenze con il tuo username, riprova per piacere"
    );

    $queries = array(
      "errors" => $errors
    );

    $queryString = http_build_query($queries);

    session_destroy();
    header("Location: index.php?" . $queryString);

  } elseif ($rows[0][3] == $myusername && $rows[0][4] == $mypassword) {
    echo '<pre>';
    var_dump($_SESSION);
    echo '</pre>';
    $_SESSION['error_username'] = null;
    $_SESSION['error_password'] = null;
    $_SESSION['login_user'] = $myusername;
    $_SESSION['user_id'] = $rows[0][0];
    $_SESSION['id'] = session_id();
    $_SESSION['time'] = time();
    $_SESSION['attempt_quantity'] = 0;

    function getrandomimage() {
      $imageplay = ['400.png', '500.png', 'sorry.png'];
      $randKeys = array_rand($imageplay, 1);
      return $_SESSION['getrandomimage'] = $imageplay[$randKeys];
    }

    getrandomimage();

    $sql2 = $pdo->prepare('SELECT * FROM myAttempts WHERE username_id = ?');
    $sql2->bindValue(1, $_SESSION['user_id'], PDO::PARAM_STR);
    $sql2->execute();

    $rows2 = $sql2->fetch(PDO::FETCH_NUM);

    if(empty($rows2)) {
      $sql = "INSERT INTO myAttempts (username_id, session_id, attempt_username, time_attempt, attempt_quantity) VALUES (?,?,?,?,?)";

      $stmt = $pdo->prepare($sql);

      $stmt->execute([$_SESSION['user_id'], $_SESSION['id'], $_SESSION['login_user'], $_SESSION['time'], $_SESSION['attempt_quantity']]);

      $pdo = null;
      $stmt = null;

    } else {
      if ($rows2[5] > 10) {
        header("location: index.php");
      } else {
        $sql3 = "UPDATE myAttempts SET attempt_quantity = attempt_quantity + 1 WHERE username_id='".$_SESSION['user_id']."'";

        $pdo->query($sql3);
        $pdo = null;
        $stmt = null;

      }

    }

  } elseif ($rows[0][3] == $myusername && $rows[0][4] !== $mypassword) {

    $errors = array(
      "password" => "la tua password potrebbe essere errata",
    );

    $queries = array(
      "errors" => $errors
    );

    $queryString = http_build_query($queries);

    session_destroy();
    header("Location: index.php?" . $queryString);
  }

} else {
  header("location: index.php");
}

?>
if(isset($_POST['submit'])) {
  var_dump(isset($_POST['submit']));
die();