Jump to content

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


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 post
Share on other sites

can you show the relevant code which is causing trouble?

Link to post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...