Solved

Cannot modify header information

Posted on 2011-09-23
20
474 Views
Last Modified: 2012-05-12

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/mailscript.inc.php");?>
<?php $page = basename($_SERVER['SCRIPT_NAME']);?>




the first two lines in the "mailscript.inc.php"
=====================================================
<?php $toadress = "mark <98@mark98.com>";
$msubject = "s-98";
..............................




the end of the lines in the "mailscript.inc.php"
===================================================
mail ($toadress,$msubject,$mcontent,$mmore, "-f info@mark98.com");
header("location:$reply");
exit;
}
?>

Open in new window

0
Comment
Question by:Refael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +2
20 Comments
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589655
you should not have echo or something before you try to change header info (including all included files, NOT JUST A FEW LINES...)
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589660
I FOUND THIS

http://www.geeklog.net/faqman/index.php?op=view&t=38

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
0
 
LVL 2

Expert Comment

by:shdwmage
ID: 36589670
Have you tried removing the seperate php entries per line?

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

The line return was killing me in the export to csv I was making.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:edster9999
edster9999 earned 50 total points
ID: 36589673
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.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589675
so, do you have any space here

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

indicated by X
0
 

Author Comment

by:Refael
ID: 36589779

Guys,

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/mailscript.inc.php" (please note that the language is German).

 
<?php $toadress = "s-98 <s-98@mark98.com>"; //   Empfänger
$msubject = "mark98"; // Betreff
$mmore = "FROM:s-98@mark98.com"; // 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 ("info@mark98.com","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;
    return;
    }
    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;
    return;
    }
	 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;
				return;
				}}
$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 info@mark98.com");
header("location:$antwort");
exit;
};?>

Open in new window

0
 
LVL 2

Assisted Solution

by:shdwmage
shdwmage earned 50 total points
ID: 36589824
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.  
0
 

Author Comment

by:Refael
ID: 36589868

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/mailscript.inc.php");?>
<?php $page = basename($_SERVER['SCRIPT_NAME']);?>
<?php include("include/header.php");?>
<?php include("include/menu.php");?><!-- end site menu --></div>
<?php include ("include/mailform.inc.php"); ?></div>

Open in new window

0
 
LVL 2

Expert Comment

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

<?php require("include/mailscript.inc.php");
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.
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589923
ok, move this line

header("location:$antwort");

to line 9 as

...
$antwort = "antwort.php";
header("location:$antwort");
...

and see if anything changes
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589951
are you sure you get this error at the end? there are a few lines using header

header("location:kontakt.php");

and

header("location:$antwort");

comment first line and see if it works, then comment out the second one...
0
 
LVL 56

Expert Comment

by:HainKurt
ID: 36589956
after commenting out all headers, use fiddler for example, to see what you get as response...
0
 

Author Comment

by:Refael
ID: 36589958

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.
0
 
LVL 2

Expert Comment

by:shdwmage
ID: 36589967
Refael,

Are you using the same contact page as well? Not just the same function.
0
 

Author Comment

by:Refael
ID: 36589975

i also comment this line, still does not work!

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

Accepted Solution

by:
Dave Baldwin earned 400 total points
ID: 36589979
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.
0
 

Author Comment

by:Refael
ID: 36589982

shdwmage:

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!
0
 

Author Comment

by:Refael
ID: 36589995


DaveBaldwin   BINGO!  

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

can you please explain?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 36590075
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 http://www.pspad.com/ 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.
0
 

Author Closing Comment

by:Refael
ID: 36590168
Thank you all guys. Special thank you to DaveBaldwin!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question