Lukas
Lukas

Reputation: 1862

PHP Location Header Causing an Error

I have the following tag for when a user is successfully logged in:

header('Location: /members');

It always used to work. Now, however, when I try to load the page, Chrome gives me this error:

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

On Firefox, I just get a blank page. When I comment the header statement out, it works, but without the redirect (obviously).

I have tried it with output buffering on and off, with the same effect. Any ideas?

Edit: I have a PHP header statement at the beginning of the code that redirects users to the member page if they are already logged in. Could this be affecting it? I'm not getting any headers already sent errors...

Upvotes: 0

Views: 1156

Answers (2)

hakre
hakre

Reputation: 197775

Install the HTTP extension and use this instead:

http_redirect('/members');

This should do the trick. If you don't have it installed. you can mimic what it does. It's explained in detail on the manual page, the steps in short:

  • Create an absolute URI from the relative URI as redirects by RFC must have one.
  • Check if headers have already been send, if so don't send header, otherwise do.
  • Display a HTTP response body with the link of the redirect.
  • Exit the script.

If you don't follow the specs, you can not expect that things work with a browser. Many examples given in the internet are not giving conform redirects, so don't be misguided by bad examples.

Next to that - as already suggested by others - turn on error reporting on your development box so you are aware of any notices and warnings that your code produces. Deal with those issues as well.

Upvotes: 0

Dan Bizdadea
Dan Bizdadea

Reputation: 1302

Just put error_reporting(E_ALL) and ini_set("display_errors", 1) and you'll see what's wrong.

Upvotes: 1

Related Questions