Jump to content

HTTP Headers & corresponding error: not working as it should?


NoUser2U

Recommended Posts

Hi, I use php's header() function for redirection users, using header('Location: some_location');In the php manual, it says:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. (http://php.net/manua...tion.header.php)
An example of incorrect usage of header() is also given in the same manual:
<html><?php/* This will give an error. Note the output* above, which is before the header() call */header('Location: http://www.example.com/');?>

BUT, if i paste the exact same code in somefile.php, and then navigate to that file, i do not get an error....Besides, i have used header() in my own site multiple times after outputting content (html elements, text), and yet no error, until today...Today, i inserted a second img using <img src=...> on the same page, and when i try to send the header('Location: ...') now, i get the error of 'headers already sent'. But if i do not include that second <img> element (so only one <img> is used on the site), then no error is given... :fool: . I am very confused because of this, the function (and it's corresponding error) is not working as it should... Can somebody please clarify?

Link to comment
Share on other sites

can you show the relevant code which is causing trouble?

Link to comment
Share on other sites

BUT, if i paste the exact same code in somefile.php, and then navigate to that file, i do not get an error....
That means that either your error reporting settings are set to not show those errors, or that your server is using output buffering by default. It doesn't mean it's correct to send output before sending a header.
Can somebody please clarify?
The rules are simple - when PHP sends output for the first time it sends all HTTP response headers. After it does that, you can't send any more headers for that response. If you use output buffering then that means that PHP holds all output until the script ends, and then it sends the headers and the output. If output buffering is off, which is the default, then it will send output as soon as you tell it to. Other than that, if you have specific questions about your code then we'll need to see your code.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...