gpt4 book ai didi

php - 错误 : localhost redirected you too many times

转载 作者:行者123 更新时间:2023-12-04 16:45:39 25 4
gpt4 key购买 nike

大家好,我是一名新生,开始专门学习 PHP 编码/编程。我尝试学习一些代码,但遇到了这个问题。

此页面不工作

本地主机重定向您的次数太多。尝试清除您的 cookie。ERR_TOO_MANY_REDIRECTS

这是我的代码:

session_start();

include('_includes/config.php');
include('_includes/db.php');

if(isset($_POST['register'])){
$_SESSION['uname'] = $_POST['uname'];
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['lname'] = $_POST['lname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['postal'] = $_POST['postal'];
$_SESSION['pass'] = $_POST['pass'];
$_SESSION['con-pass'] = $_POST['con-pass'];
}

if(strlen($_POST['uname'])<3){
header("Location:register.php?err=" . urlencode("The username must be at least 3 characters long"));
die();
}

我真的不知道该怎么办我在 php 中遇到了一些错误,但我没有遇到过这种错误请帮助并告诉我我做错了什么。

最佳答案

检查用户是否请求注册而不是重定向,更新代码如下:

session_start();

include('_includes/config.php');
include('_includes/db.php');

if(isset($_POST['register'])){
$_SESSION['uname'] = $_POST['uname'];
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['lname'] = $_POST['lname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['postal'] = $_POST['postal'];
$_SESSION['pass'] = $_POST['pass'];
$_SESSION['con-pass'] = $_POST['con-pass'];
}

if(strlen($_POST['uname'])<3 && isset($_POST['register'])){ // add && isset($_POST['register'])
header("Location:register.php?err=" . urlencode("The username must be at least 3 characters long"));
die();
}

注意:我建议您不要重定向用户以在某些文件中显示错误消息!您可以将错误消息存储在 vars 中并检查错误 var 是否不为空回显它!

session_start();

include('_includes/config.php');
include('_includes/db.php');
$error = ''; //add this var
if(isset($_POST['register'])){
$_SESSION['uname'] = $_POST['uname'];
$_SESSION['fname'] = $_POST['fname'];
$_SESSION['lname'] = $_POST['lname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['postal'] = $_POST['postal'];
$_SESSION['pass'] = $_POST['pass'];
$_SESSION['con-pass'] = $_POST['con-pass'];
}

if(strlen($_POST['uname'])<3 && isset($_POST['register'])){ // add && isset($_POST['register'])
/*header("Location:register.php?err=" . urlencode("The username must be at least 3 characters long"));
die();*/
$error = 'this is error message';
}
//add below code anywhere you want show error
if($error){
echo $error;
}

关于php - 错误 : localhost redirected you too many times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631081/

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