Cannot modify header information

Hello Experts,

I have a form to email in php. i have the same form and the same script in another website in the same server and it works great. only this one site is generating the "Warning: Cannot modify header information - headers already sent by...".

i know you should check for blank spaces but it does not help.

the contact.php first line has a "php require" to include the for to mail script.
the warning indicates that this line produces the problem.

...sent by (output started at /usr/www/users/geg/project/98/contact.php:1)

in the required file at the very end of the script i use the "header location".

i am attaching peaces of the code below:

the two first lines in the contact.php
<?php require("include/");?>
<?php $page = basename($_SERVER['SCRIPT_NAME']);?>

the first two lines in the ""
<?php $toadress = "mark <>";
$msubject = "s-98";

the end of the lines in the ""
mail ($toadress,$msubject,$mcontent,$mmore, "-f");

Open in new window

Who is Participating?
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
If you are using UTF-8 character encoding, sometime the BOM or Byte Order Mark will be put in the beginning of the file.  It will be invisible in a UTF-8 aware editor but not to PHP and the web server.  See if you get 'funny characters' at the beginning of the file in Latin or ASCII format.
HainKurtSr. System AnalystCommented:
you should not have echo or something before you try to change header info (including all included files, NOT JUST A FEW LINES...)
HainKurtSr. System AnalystCommented:

The fix is, obviously, to remove that whitespace from the file. Read the error message carefully. It says "output started at ..." followed by a file name and a line number. That is the file (and line) that you need to edit. Ignore the second file name - that is only a file that included the file that has the whitespace. The first file is the one you have to edit, not the second one
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Have you tried removing the seperate php entries per line?

<?php require("include/");
$page = basename($_SERVER['SCRIPT_NAME']);?>

The line return was killing me in the export to csv I was making.
edster9999Connect With a Mentor Commented:
You have two options.  
1. Don't output any body text until you make the change to the header.
2. Set Buffering to 'on' in the php.ini then it buffers the output before sending it and even if you have output body text changes to the header still go up to the top and work.
HainKurtSr. System AnalystCommented:
so, do you have any space here

<?php require("include/");?>XXXXXXXXXXX
<?php $page = basename($_SERVER['SCRIPT_NAME']);?>XXXXXXXXXXXXXXX

indicated by X
RefaelAuthor Commented:


I wrote that i am aware of white spaces and i have removed them and double checked!!!

shdwmage: i tried that. is does not help too.

Here is the entire "include/" (please note that the language is German).

<?php $toadress = "s-98 <>"; //   Empfänger
$msubject = "mark98"; // Betreff
$mmore = ""; // From, BCC, etc.
$mmore .= "\n";
$mmore .= "MIME-Version: 1.0\n";
$mmore .= "Content-type: text/plain; charset=utf-8\n";
$mmore .= "Content-Transfer-Encoding: quoted-printable\n";
$antwort = "antwort.php";
if ($_POST['zuruecksetzen']){header("location:kontakt.php");exit;}
if ($_POST['Suchprofil']){ # VERARBEITUNG
$s_Firma = $_POST['Firma'];
$s_Name = $_POST['Name'];
$s_Vorname = $_POST['Vorname'];
$s_Titel = $_POST['Titel'];
$s_Strasse_Nr = $_POST['Strasse_Nr'];
$s_Postleitzahl = $_POST['Postleitzahl'];
$s_Ort = $_POST['Ort'];
$s_Telefon = $_POST['Telefon'];
$s_Mobil = $_POST['Mobil'];
$s_Fax = $_POST['Fax'];
$s_Email = $_POST['Email'];
$s_Erdgeschosswohnung = $_POST['Erdgeschosswohnung'];
$s_Etagenwohnung = $_POST['Etagenwohnung'];
$s_Dachgeschosswohnung = $_POST['Dachgeschosswohnung'];
$s_Penthouse = $_POST['Penthouse'];
$s_Loft = $_POST['Loft'];
$s_Remise = $_POST['Remise'];
$s_Zimmer_min = $_POST['Zimmer_min'];
$s_Zimmer_max = $_POST['Zimmer_max'];
$s_qm_min = $_POST['qm_min'];
$s_qm_max = $_POST['qm_max'];
$s_Kaufpreis = $_POST['Kaufpreis_max'];
$s_Eigennutzer = $_POST['Eigennutzer'];
$s_Kapitalanleger = $_POST['Kapitalanleger'];
$s_Terrasse = $_POST['Terrasse'];
$s_Tiefgaragenstellplatz = $_POST['Tiefgaragenstellplatz'];
$s_Balkon = $_POST['Balkon'];
$s_Garten = $_POST['Garten'];
$s_Bemerkungen = $_POST['Bemerkungen'];
$s_Newsletter  = $_POST['Newsletter'];
$go = $_POST[go];
$name = strip_tags($_POST[name]);
$email = strip_tags($_POST[email]);
$host = $_SERVER['HTTP_HOST'];
$field = $s_Firma . $s_Name  . $s_Vorname . $s_Titel . $s_Strasse_Nr . $s_Postleitzahl . $s_Ort . $s_Telefon . $s_Mobil . $s_Fax . $s_Email . $s_Erdgeschosswohnung . $s_Etagenwohnungwohnung . $s_Dachgeschosswohnung . $s_Penthouse . $s_Loft . $s_Remise  . $s_Zimmer_min . $s_Zimmer_max . $s_qm_min . $s_qm_max . $s_Kaufpreis . $s_Eigennutzer . $s_Kapitalanleger . $s_Terrasse . $s_Tiefgaragenstellplatz . $s_Balkon . $s_Garten  . $s_Bemerkungen . $s_Newsletter;
if (preg_match("/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i", $field))
{	mail ("","FORMSPAM VERSUCH $host","\n-","from:form@$host");
	die("falsche Eingabe!"); 
    if ($s_Name == '')
    {$meldung = '<span style="color:red;">Bitte tragen Sie mindestens Ihren Namen und eine Kontaktm&ouml;glichkeit ein.</span>';
    $mailok = 0;
    if ($s_Email == '' && $s_Telefon == '' && $s_Mobil == '' )
    {$meldung = 'Um Kontakt mit Ihnen aufzunehmen, benötigen wir entweder Ihre Telefonnummer (Festnetz oder Mobil) oder Ihre E-Mail Adresse.';
    $mailok = 0;
	 if ($s_Email !='')
         if (!preg_match("/^[-^!#$%&'*+\/=?`{|}~.\w]+@"
                       . "([-a-zA-Z0-9]+\.)[-a-zA-Z0-9]{2,4}$/",$s_Email))
                    {$meldung = 'Die Angabe der E-Mail Adresse scheint einen Fehler zu enthalten.<br>
        	 Bitte überprüfen Sie Ihre Eingabe.';
		    $mailok = 0;
$mailcontent = array(
'Firma' => $s_Firma,
'Titel' => $s_Titel,
'Vorname' => $s_Vorname,
'Name' => $s_Name,
'Strasse, Nr' => $s_Strasse_Nr,
'Postleitzahl' => $s_Postleitzahl,
'Ort' => $s_Ort,
'Telefon' => $s_Telefon,
'Mobil' => $s_Mobil,
'Fax' => $s_Fax,
'Email' => $s_Email,
'Erdgeschosswohnung' => $s_Erdgeschosswohnung,
'Etagenwohnung' => $s_Etagenwohnung,
'Dachgeschosswohnung' => $s_Dachgeschosswohnung,
'Penthouse' => $s_Penthouse,
'Loft' => $s_Loft,
'Maisonette' => $s_Remise,
'Zimmer_min' => $s_Zimmer_min,
'Zimmer_max' => $s_Zimmer_max,
'qm_min' => $s_qm_min,
'qm_max' => $s_qm_max,
'Max.Kaufpreis' => $s_Kaufpreis_max,
'Eigennutzer' => $s_Eigennutzer,
'Kapitalanleger' => $s_Kapitalanleger,
'Terrasse' => $s_Terrasse,
'Tiefgaragenstellplatz' => $s_Tiefgaragenstellplatz,
'Balkon' => $s_Balkon,
'Garten' => $s_Garten,
'Bemerkungen' => $s_Bemerkungen,
'Newsletter' => $s_Newsletter
$mcontent = 'Suchprofil-Anfrage Schönhauser 98.';
foreach ($mailcontent AS $t => $wert) {
if( $wert !='') {
    if( $wert == 'x01') {$mcontent .= '
    ' . $t;}
	 elseif ($t == 'Bemerkungen') {$mcontent .= 'Bemerkungen:--------' . $wert . '--------';
	 } else { $mcontent .= '' . $t . ': 	'. $wert; }}}
mail ($toadress,$msubject,$mcontent,$mmore, "-f");

Open in new window

shdwmageConnect With a Mentor Commented:
What about your contact.php file?  If you are including this file which is fine, how is your contact.php?  The message you listed above seems to say the problem exists there.

...sent by (output started at /usr/www/users/geg/project/98/contact.php:1)

When I build a webpage, I actually store the whole page to a variable and then print it out.  This way I have complete control over how the whole page is put together.  
RefaelAuthor Commented:

Hi shdwmage:

here is the contact php file (i removed the HTML script and left only the PHP)
the 1 line in the contact.php is the first line here in the code as well.. as stated above.

<?php require("include/");?>
<?php $page = basename($_SERVER['SCRIPT_NAME']);?>
<?php include("include/header.php");?>
<?php include("include/menu.php");?><!-- end site menu --></div>
<?php include ("include/"); ?></div>

Open in new window

Can you try as a testing method to do something like the following in a test file:

<?php require("include/");
echo "Test page";?>

Open in new window

When I come across these type of issues I work line by line until I get the error to pop up and then evaluate how its done.

I have a feeling your server is sending the header information before processing the php because of the HTML code that is in the file.
HainKurtSr. System AnalystCommented:
ok, move this line


to line 9 as

$antwort = "antwort.php";

and see if anything changes
HainKurtSr. System AnalystCommented:
are you sure you get this error at the end? there are a few lines using header




comment first line and see if it works, then comment out the second one...
HainKurtSr. System AnalystCommented:
after commenting out all headers, use fiddler for example, to see what you get as response...
RefaelAuthor Commented:

i moved it....
still points to contact.php line 1 warning.

the funny thing is, in another site on the same server, different folder i have the same script. i works there no problem only here i face this problem.

Are you using the same contact page as well? Not just the same function.
RefaelAuthor Commented:

i also comment this line, still does not work!

//if ($_POST['zuruecksetzen']){header("location:kontakt.php");exit;}   (line number 9).
RefaelAuthor Commented:


Basically its the same contact page and the required script for the sub sites, yes!
the only changes are in the HTML not in the PHP!
RefaelAuthor Commented:

DaveBaldwin   BINGO!  

i just changed it to "DaveBaldwin to charset=ISO-8859-1  FROM charset=utf-8

can you please explain?
Dave BaldwinFixer of ProblemsCommented:
My 'guess' is that some file had the BOM at the beginning or was creating it.   And since that normally goes at the Very Beginning of the file, it was appearing ahead of the PHP tag in one of those files or in some white space that you couldn't see.

I use PSPad to investigate and edit things like this because you can switch character sets and even look at a HEX display of the file if you need to.
RefaelAuthor Commented:
Thank you all guys. Special thank you to DaveBaldwin!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.