Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Warning: Cannot modify header information

Posted by julian (julian), 8 October 2005
I have not used this forum before thus am v new to this type of thing....
I have a few questions regarding the use of headers and also the warnings produced by php!
I have written some php scripts that interface with a mysql database that is running on a live server, but before putting the script on the live server i wrote it to work on my own apache server at home, and it workes fine at home.
However on the live server the script produces an error from the php and that error is;

Warning: Cannot modify header information - headers already sent by (output started at /home/impactre/public_html/php files being tested/insert_modorganisationslive.php:11) in /home/impactre/public_html/php files being tested/insert_modorganisationslive.php on line 26.

my intention at line 26 of the php code was to try to force the webserver to reload the page... by putting the line:
header("Location: insert_rec_into_organisationslive.html");

I think that this will reload the script that processes the data submitted by the form??
Bassically I have an html  web page that presents a form, method post that has the action

my intention is for the web page with the form in to gather information from the user, and the file insert_modorganisationslive.php to place that information in to a database table and then having done this send a mesage to the screen saying the record has been added to the database, and then to allow the user to either add a further record or to go to another main menu page?

I am a little muddled I think...... is it not true that a form with a submit button in it will (when the submit button is pressed) send a header to the web server? what happens if another header is send afterwards??
How can I tell if a header has been sent and under what circumstances does this php warning get produced?

Perhaps after the php script has delt with the data processing  & has inserted the record in the database and told the user this has happened, I should force the script to load the web page that asks for some more data to put in to the database instead of trying to reload the file that does the processing of that data

Am I perhaps usinf the function header("Location:blar blar"); in the wrong way??

any help would be appreciated as would any advice as to what to read or where to look for help on headers and the use of..? I will be experimenting with the file that caused the error and try to see if I can sort this out myself?
What is the usual way in php to leave the page loaded and to force a load of either the same page or another page??
many thanks in advance

Posted by admin (Graham Ellis), 8 October 2005
Hi, Julian, and welcome to Opentalk.

Web responses (via the HTTP Protocol) consist of a header and content ... where the content is typically HTML (and may, confusingly, contain a head).

PHP's header function generates information to include in the header and so it must be called only before even a single byte of other information is sent out from your piece of PHP.  This is because PHP assumes ... as soon as it sees any output via print, echo or of regular HTML .... that the header information is complete and can be sent.

If you want to generate HTML before you send out the header, do so and save it into a variable that you print out later on after you've sent the header.

Footnotes ...

1. The headers referred to about are outgoing headers FROM the server.  Each http request made by a browser includes an incoming header, which isn't specifically relevant to the subject above

2.  Even a <html> open tag send out before your header(...) will cause the problem you have described.

3. You ask about documentation; sometimes hard to track down what you're looking for ... I use Chris Shiflett's Book as my reference, though perhaps it's not an ideal beginner's text.

To resubmit further data back to a page, by the way, you can use a form tag without an action= section.  Others will tell you to use $PHP_SELF to include the name of the page itself.   Do NOT hardcode the page name in, since that will mean that you have to change the script if you rename it in the future.

Posted by curtis (curtis), 8 October 2005
That was a very informative post, Graham

Hi Julian. If your script works at home, it's most likely because you have output bufferring enabled in your php.ini file. If you want to have the page reload using header(), then you would need to specify the condition under which it should reload before any output:
if ( some condition ) {
   header('Location: ' . $_SERVER['PHP_SELF']);



Posted by julian (julian), 9 October 2005
I will have to see if I do have output buffering enabled in my php ini file on my php engine that I am running at home, (which is php ver 5.0.4), plus also see if output buffering is enabled on the live web server as well, ( which is php version 4.3.11
In the second case I will have to ask the hosting company to provide this information and in the first case I will look at the php ini file on my computer at home!!
Once I have found the answers to both of these questions I will put it as an answer to this particular post>>>>>.
I also need to understand what output buffering actually does do? and what exactly is the effect of having it switched on and having it switched off??
I also need to get a widder understanding of what happens when a header is sent from the server to the client machine, at what point when a page is served out by the web server to a client computer is the header sent precisely...... and finally what does happen when a header is sent out from a server and in the offending script there is a header in the <head> tags and a another header in the <body> tags aswell?<<><><

I have orderedHTTP Developers Hand book by Chris Shifett and when I get I will try to read it and understand more about headers.....

I have for the time being bypassed the problem of getting the php warning by changing the php so that there is no call to the header function at all....

Now the code reads

if (mysql_query($sql, $conn)) {
      echo "record added!";
      $display_block = "<h1>this is a display block</h1>
      <a href=\"newmenuchoicelive.php\">Go back to Menu</a>
      <a href=\"insert_rec_into_organisationslive.html\">Insert another Record</a>
      echo $display_block;
} else {
     echo "something went wrong";

we now have a couple of links to other pages placed in the output of the php and sent to the screen... ultimately of course the page that the user is looking at!

Thanks very much for your answer curtis and graham.

Posted by admin (Graham Ellis), 9 October 2005
Julian, if you're outputting a location header, then I don't think you should be outputting any page content at all ... and I'm not sure that I understand why you're wanting to send out a location header in the first place ...  

If you include a location in your header, then that's in place of any page content - so you whole script needs to be in between a <?php and a ?> tag, and apart from other headers you can't send anything else out meaningfully.   In effect, a location header says "this isn't the page you want ... that page is really at xxxx".

Output buffering is a different issue.  If you're generating a response (header + body), by default each time you call a header function or print something it gets sent out straight away.  That's good and clean, but it means that your PHP code can't come back later and say "oops - I should have sent this earlier" which is in effect what you try and do if you use a header call once you've sent some content output.  Output buffering should be irellevant in your case - if you're sending out a location then that should be ALL you're sending out!.

As an aside, it's far better to write code so that it sends out all the header before it sends out any content rather than relying on buffering.   That way, the code's more portable, more efficient, and cleaner.  Like I say - that's an aside because with a location header it's irellevant - no body!  

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho