错误:localhost重定向了你太多次了

时间:2017-08-11 09:01:32

标签: php

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

此页面无效

localhost重定向了你太多次了。 尝试清除您的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中遇到了一些错误,但我没有遇到过这种错误请帮助我,并请我做错了。

1 个答案:

答案 0 :(得分:3)

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

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中存储错误信息并检查error 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;
}