Too many redirects

AAA picture AAA · Jan 28, 2011 · Viewed 40.7k times · Source

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

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.

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?

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:

       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.

Answer

Prisoner picture Prisoner · Jan 28, 2011

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()).