gpt4 book ai didi

php - 错误太多重定向

转载 作者:可可西里 更新时间:2023-11-01 16:40:27 24 4
gpt4 key购买 nike

尝试创建我的 server.php 脚本,所以直到现在一切都很好。我想防止重新提交表单,并将 header('location: index.php'); 添加到我的脚本中。然后我遇到了问题:ERR_TOO_MANY_REDIRECTS。正如你们中的许多人已经了解的那样,我的数据库中充满了垃圾。所以,这是我的代码:

<?php

$username = $email = $password = "";
$usernameErr = $emailErr = $passwordErr = "";
$servername = 'localhost';
$serveruser = 'root';
$serverpassword = 'root';
$db = 'example';

$conn = new mysqli($servername, $serveruser, $serverpassword, $db);

if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}

if(isset($_POST['register'])) {
$username = mysqli_real_escape_string($conn,$_POST['username']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$password = mysqli_real_escape_string($conn,$_POST['password']);

if(empty($username)) {
$usernameErr = "Username is required";
} else {
$username = test_input($_POST["username"]);

if(!preg_match("/^[a-zA-z ]*$/", $username)){
$usernameErr = "Only letters and whitespaces allowed";
}

}

if(empty($email)) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);

if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Wrong email format";
}

}

if(empty($password)) {
$passwordErr = "Password required";
} else {
$password = test_input($_POST["password"]);
}
}

if ($usernameErr == "" && $emailErr == "" && $passwordErr == "") {
$sql = "INSERT INTO users (username, email, password)
VALUES('$username','$email','$password')";

if($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

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

function test_input($data) {
$data = trim($data);
$data = htmlspecialchars($data);
$data = stripslashes($data);
return $data;
}

?>

最佳答案

为了防止TOO MANY REDIRECT 放置这段代码

if ($usernameErr == "" && $emailErr == "" && $passwordErr == "") {
$sql = "INSERT INTO users (username, email, password)
VALUES('$username','$email','$password')";

if($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

header("location: index.php");

if(isset($_POST['register'])) { //.................}

检查错误后的这个 block

并且为了防止重新提交表单,请使用此问题的已接受答案

Preventing form resubmission

关于php - 错误太多重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com