php 重定向过多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4831152/
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-25 14:30:57  来源:igfitidea点击:

Too many redirects

phpsessionredirectsession-variablesisset

提问by AAA

I can't find the solution to this one. Basically we are using a vanity url system so its user.domain.com. All files are accessed like user.domain.com/home.php etc. When you clear cookies the redirects work, it prompts them to login again. But when i use logout, it still works but when they go back to the link to login, (user.domain.com, has a login form on the landing page for the user) it won't work. The error i get is

我找不到解决此问题的方法。基本上我们使用的是一个虚名 url 系统,所以它的 user.domain.com。所有文件都可以像 user.domain.com/home.php 等一样访问。当您清除 cookie 时,重定向工作,它会提示他们再次登录。但是当我使用注销时,它仍然有效,但是当他们返回登录链接时,(user.domain.com,在用户登录页面上有一个登录表单)它不起作用。我得到的错误是

Error Message:

错误信息:

              The page isn't redirecting properly



               Firefox has detected that the server is redirecting the request for this address
               in a way that will never complete.


               This problem can sometimes be caused by disabling or refusing to accept
      cookies.

Redirect Code for all member pages:

所有会员页面的重定向代码:

         if(!isset($_SESSION['user_name'])) { header("Location: http://$_SERVER[HTTP_HOST]");}
         Note:  The $_SERVER[HTTP_HOST] captures the user.domain.com value for redirect. 

Logout code:

登出代码:

          <?php


            session_start();
            session_destroy();

            header( 'Location: $_SERVER[HTTP_HOST]' ) ;
              ?>

The only way for this issue to go away is if users clear their cookies.

消除此问题的唯一方法是用户清除其 cookie。

UPDATE: After using the logout.php i went to one of the member pages user.domain.com/home.php instead of it redirecting to user.domain.com for login it gave me the redirect error. Could this be an issue with the sessions or something with the headers?

更新:使用 logout.php 后,我转到成员页面 user.domain.com/home.php 之一,而不是重定向到 user.domain.com 进行登录,它给了我重定向错误。这可能是会话的问题还是标题的问题?

What can i enhance or add to fix this? I have tried Googling but haven't found anything particular to this. I really need some help in fixing this. Thanks.

我可以增强或添加什么来解决这个问题?我试过谷歌搜索,但没有发现任何特别的东西。我真的需要一些帮助来解决这个问题。谢谢。

This is the error message i get in Safari:

这是我在 Safari 中收到的错误消息:

       Too many redirects occurred trying to open “http://user.domain.com/home.php”.
      This might occur if you open a page that is redirected to open another page which then is  redirected to open the original page.

回答by Prisoner

The browser is stopping you from hammering the server with a bunch of requests. This is most likely due to the header()sending you to a page which in turn sends you to the same page (or page with the same header()).

浏览器阻止你用一堆请求来攻击服务器。这很可能是由于header()将您发送到一个页面,而该页面又将您发送到同一页面(或具有相同 的页面header())。

回答by Josh K

This is happening most likely because you are being redirected to the same logout page. I would try setting the location differently.

这很可能是因为您被重定向到同一个注销页面。我会尝试以不同的方式设置位置。

回答by thedrs

Happened to me when i was redirecting back to the same page with the same parameterswhich would cause in infinite loop of requests to the same page by the browser if the browser would not stop on the second iteration and give this error.

当我使用相同的参数重定向回同一页面时发生在我身上,如果浏览器不会在第二次迭代时停止并给出此错误,则会导致浏览器无限循环请求到同一页面。

Best bet - debug your code and see if you are reentering the page with the same query strings/post params ...

最好的选择 - 调试你的代码,看看你是否使用相同的查询字符串/发布参数重新进入页面......

回答by cormacio100

The problem could lie in the page that you are forwarding to rather than the page you are forwarding from This happened to me when the page I was forwarding to (home.php) from a page forwardingPage.php failed an authentication check.

问题可能在于您要转发到的页面而不是您要转发的页面 当我从页面 forwardingPage.php 转发到 (home.php) 的页面未通过身份验证检查时,这发生在我身上。

Format on the receiving home.php page was

接收 home.php 页面上的格式是

if authentication check passes...do this else if authentication check fails use header function to forward back to the same forwardingPage.php

如果身份验证检查通过...请执行此操作,否则如果身份验证检查失败,请使用标头函数转发回相同的 forwardingPage.php

The authentication check was failing every time so the page got forwarded and then back to itself. I fixed the authentication check and problem solved

身份验证检查每次都失败,因此页面被转发然后返回到自身。我修复了身份验证检查并解决了问题