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

您好我是一名新生并开始学习PHP编程/编程。 我尝试学习一些代码,但我遇到了这个问题。</ p>

此页面无法正常工作</ strong> </ p>

localhost重定向了你太多次了。</ em>
尝试清除你的cookie。
ERR_TOO_MANY_REDIRECTS </ strong> </ p>

这是我的代码:</ p>

  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'])&lt; 3){
header(“Location:register.php?err =”。urlencode(“用户名必须是 至少3个字符长“));
die();
}
</ code> </ pre>
\ n

我真的不知道该怎么办我在php中遇到了一些错误但是我没有遇到过这种错误请帮助我,并请我明白我做错了什么。</ p>
< / DIV>

展开原文

原文

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.

php
doutucui0133
doutucui0133 是的,代码是register.php,
大约 3 年之前 回复
dongshao2967
dongshao2967 这是code.php文件中的代码吗?
大约 3 年之前 回复
dongnai5905
dongnai5905 代码工作!非常感谢你!我肯定在我的笔记中写这个!
大约 3 年之前 回复
dongzhina7098
dongzhina7098 关于我该怎么办的任何建议?很抱歉打扰我这是我第一次做的一切。
大约 3 年之前 回复
douren6035
douren6035 没有测试,但这可能会解决您的问题。将(strlen($_POST['uname'])<3)更改为if(isset($_POST['uname'])&&strlen($_POST['uname'])<3)以禁用重复重定向。
大约 3 年之前 回复
doolo00026
doolo00026 您每次都输入if,因此您总是被重定向。
大约 3 年之前 回复

1个回答



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

  session_start(); 

include('_ includes / config.php');
include('_ includes / db.php');

if(isset($ _ POST ['register'])){\ n $ _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'])&lt; 3&amp;&amp; isset($ _ POST ['register'])){// add&amp;&amp; isset($ _ POST ['register'])
header(“Location:register.php?err =”。urlencode(“用户名长度必须至少为3个字符”));
die();
}

</ code> </ pre>

注意:</ strong>我建议您不要重定向用户以显示错误消息,如果某些文件中的代码! 你可以在vars中存储错误信息并检查错误var是否为空回显它!</ p>

  session_start(); 

include('_ includes / config.php');

include('_ includes / db.php');
$ error =''; //添加此var
if(isset($ _ POST ['register'])){
$ _SESSION ['uname'] = $ _POST ['uname'];
$ _SESSION ['fname'] = $ _POST ['fname'];
$ _SESSION ['lname'] = $ _POST ['lname'];
$ _SESSION ['email'] = $ _POST ['email'];
$ _SESSION ['地址 '] = $ _POST ['address'];
$ _SESSION ['postal'] = $ _POST ['postal'];
$ _SESSION ['pass'] = $ _POST ['pass'];
$ _SESSION ['con-pass'] = $ _POST ['con-pass'];
}

if(strlen($ _ POST ['uname'])&lt; 3&amp;&amp; isset($ _ POST [ 'register'])){// add&amp;&amp; isset($ _ POST ['register'])
/*header ("Location:register.php?err=“。urlencode(”用户名长度必须至少为3个字符“));
die(); * /
$ error ='这是错误信息';
}
//在您想要的任何地方添加以下代码显示错误
if($ error){
echo $ error;
}
</ code> </ pre>
</ div>

展开原文

原文

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

donglanfu5831
donglanfu5831 完成! 非常感谢你!
大约 3 年之前 回复
dtczp02204
dtczp02204 您的欢迎,如果这个答案对您有所帮助,请将答案作为真实答案(以下投票)
大约 3 年之前 回复
dongzhe3171
dongzhe3171 就像魅力先生一样,非常感谢你和我这样的初学者分享你的知识。
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐