douwei3172 2017-08-11 09:01
浏览 920
已采纳

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

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.

This page isn’t working

localhost redirected you too many times. Try clearing your cookies. 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.

  • 写回答

1条回答 默认 最新

  • duanpao4172 2017-08-11 09:12
    关注

    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!

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?