php 错误:本地主机重定向你太多次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45631081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:07:43  来源:igfitidea点击:

ERROR: localhost redirected you too many times

php

提问by missNobody

Hi I'm a new student and starting to learn coding/programming specially on PHP. I tried learning some code and I have encountered this problem.

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

This page isn't working

此页面无效

localhost redirected you too many times.Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

localhost 将您重定向了太多次。尝试清除您的 cookie。 ERR_TOO_MANY_REDIRECTS

and this is my code:

这是我的代码:

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();
    }

I really don't know what to do I have encountered some errors in php but I haven't encountered this kind of error PLEASE HELP and PLEASE ENLIGHTEN me on what I have done wrong.

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

回答by aidinMC

Check if user request to register too than redirect, update code like below :

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

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();
    }

Note:at all i'm suggest you don't redirect user to show error message if codes in some file! you can store error message in vars and check if error var is not empty echo it!

注意:如果某些文件中有代码,我建议您不要重定向用户以显示错误消息!您可以将错误消息存储在 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;
}