Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Warning: Cannot modify header information 1

Status
Not open for further replies.

BizzyLizzy

Technical User
Nov 3, 2003
77
AU
Please can somebody help me with this problem. I have had to upload my website again after some problems on the host server. Having done all that I now find that my contact form does not work anymore. I get the following error.

Warning: Cannot modify header information - headers already sent by (output started at /home/intragis/public_html/index.php:8) in /home/intragis/public_html/contact2.php on line 87

I have read the FAQ and have to say that I didnt really understand it :( (lol you may have guessed by now that I am no programmer!).

I have looked for whitespaces and cant see any so I dont think that is the problem.

line 8 of index.php is as follows:-

<META name="keywords" content="GIS viewer, GIS browser, GIS software, maps, mapping, GIS data browser, remote sensing images, ESRI, ECW IntraGIS, enterprise, corporate, TatukGIS, GIS mapping software, GIS, geographic information systems, Web services, Getting Started, executives, IT professionals, developers, education, GIS practictioners, government, new users, GIS.com, desktop GIS, server GIS, developer GIS, mobile GIS, GIS Web services, australia, western, systems"><!--end keywords-->

so I think that could be a red herring.

What I dont understand is why this has suddenly stopped working. I have not changed anything - all I have done is re-upload the site.

If you need me to post the code for the contact form please let me know.

Hoping someone can help.

Cheers Lizzy
 
Sorry I should have pointed out that this works fine on my test server but not when I upload it to the host site.

 
the rule is that you must not send ANY data to the browser before you try and send the headers. it is not just whitespace although that is the most common culprit.

if it worked on one machine but not another then the configurations of the machines MUST be different. I suspect, in this case, one machine was running automatic output buffering.

best solution is to redesign. worst is to switch output buffering back on.
 
Thanks for the reply - trouble is yes there is likely configuration differences between the two machines as we host our website with our ISP (we dont have our own webserver). So cant change the configuration at that end unfortunately. lol not that I would necessarily want output buffering on after reading your many comments on the matter :).

What I really don't understand is that last week it worked fine. This week it doesn't. I have contacted our hosters and they assure me that nothing has changed at their end so I just dont get it.

Ok so a redesign is in order then. All I am trying to do is a simple contact form with some error checking etc. that then redirects to a thank you page once the page has been submitted. The problem is that the redirect isn't working any more. I will try to redesign so that I dont have to use to use the header() bit anymore but I am not awfully confident about doing this :).

Thanks anyway

Lizzy
 
redirects are ok if you want to use them. personally i prefer a dispatch method where a single page does more or less everything for a site and the internal logic works out what to say to the user.

the bit that is going wrong in your app appears to be that you are including the index.htm at some point in your code before you perform the redirect. alternatively you could be including the contact.php page at some stage after outputting the meta tags.

i am not against output buffering either. i use it in several scenarios, but to switch it back on only to correct a design defect would be weird. however, if you are confident that the behaviour is not a defect but a necessary evil then go with ob.

most hosts allow you to upload your own php.ini file in which you can specify local config settings. you can also turn output buffering on in the script itself.
 
lol I asked whether I could upload my own php ini file as they have register globals switched on and other nice little security holes but was told in no uncertain terms that they would not allow this and it would in itself constitute a security risk!!

You are right in that I am including the contact.php after the meta tags (well thats how the site was designed anyway - not by me I might add). I am a little reluctant to redesign the index page totally at the moment due to time constraints. So for the time being I will probably go with ob if I can figure out where in the code I need to put it :). lol this has been one steep learning curve - give me back Crystal reports and databases anytime!

Thanks for your help :)

Lizzy
 
add
Code:
<?php ob_start(); ?>
to the first line of the index.php file.

and move hosts.
 
definitely considering changing hosts - we have been having problems with them for a while now - unfortunately this isnt my decision to make but will certainly talk to the boss about it.

So if I put the ob start on the 1st line of the index.php do I need to put a ob end at the end of the php in that file?

 
no. php will automatically flush the output buffer at the end of the script. if you want to do this manually you can use a number of different functions depending on what you want to achieve.
 
cool ok thanks will try that out and let you know how I get on :D.

 
Wooohooooo

Thanks that did it :). This will do until I can find the time to do a redesign of the website.

Thank you so much :).

 
arrrrgh spoke to soon! lol not getting the error anymore but the contact form is not sending emails either.

any more suggestions? Or do I say stuff it and start the grand rewrite!
 
unlikely to be related. check the php.ini settings for the right mail server and/or path to sendmail.

if the solution is not easy, download and use phpmailer from sourceforge. it has its own smtp class which avoid these problems.
 
Emails from other forms are working fine. This happened after I loaded the form up to my host site. Its only the contact form which has been having the problem.

 
Sorry meant to say again working fine on the test server just not on the upload. Another configuration difference in their php ini perhaps?
 
what is the code you are using to send the mail? and the code to generate the form?
 
thanks for putting up with my newbie problems jpadie. Here is the code for the contact form. Its nothing very complicated.

Code:
<?php
if ( $_POST['submit'] ) {

$valid = 8768;

$name = $_POST['name'];
if ( empty($name) ) {
$valid = 3534;
$name_error = 'You did not enter your name';
}

$company = $_POST['company'];

if ( empty($company) ) {
$valid = 3534;
$company_error = 'You did not enter your company';
}

$email = $_POST['email'];

//EMAIL VERIFICATION

function CheckMail($email) // this function checks if the email format is ok, 
//if the chars are allowed, if there are enough chars in the TLD learn more 
//about eregi function here: [URL unfurl="true"]http://www.php.net/manual/en/function.eregi.php[/URL]
{
if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$",$email)){
		return true;
	}
	else 
	{ 
		return false; 
	}
}
if ((empty($email)) || (!CheckMail($email))) //here we check if the email is 
//empty and if the previous function controls are passed, if there are 
//errors the user is sent to a previously prepared custom error page
{
  	$valid=3534;
	$email_error = 'Please enter a valid Email address';
}

	$domains = array('@dodo','@hotmail', '@yahoo', '@gmail', '@aol','@TEE','@apat','@interfere','@inwind','@isiline','@izsmportici','@libero','@liero','@rai','@tin','@tinet','@tiscali');
	$domain = strstr($email,'@'); 
	$domain2 = explode('.', $domain);
	if (in_array($domain2[0], $domains)) {
	
	
	$valid = 3534;
	$email_error = 'Sorry, Internet Email accounts are not allowed';
}
	

$tel = $_POST['telephone']; 
$fax = $_POST['fax'];

$subject = $_POST['subject'];

if ( empty($subject) ) {
$valid = 3534;
$subject_error = 'You did not enter a subject';
}

$comments = $_POST['comments'];

if ( empty($comments) ) {
$valid = 3534;
$comments_error = 'You did not enter any comments';
}

$dept = $_POST['D1'];
$deptadd = "{$dept}@myserver.com.au";

$message .= 'Name: ' . $name . "\n";
$message .= 'Company: ' . $company . "\n";
$message .= 'Email: ' . $email . "\n";
$message .= 'Tel: ' . $tel . "\n";
$message .= 'Fax: ' . $fax . "\n\n";
$message .= 'Comments: ' . "\n";
$message .= $comments . "\n";

if ( $valid == 8768 ) {

$sendto = $deptadd;
$headers = 'From: Contact Form <' . $email . '>' . "\r\n" . 'Reply-To: ' . $email . "\r\n";
mail($sendto, $subject, $message, $headers);
header ("Location: index.php?view=contact_thanks");
}
}
?>
<html>
<form action="index.php?view=contact2" method="post">
<font size="2">
<label for="name">
<p></p><img border="0" src="image_folder1/contactus2.jpg" width="117" height="26"></label></font><p>&nbsp;</p>
<table border="0" width="100%" id="table1">
	<tr>
		<td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="name">Name:  </label>
        </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="name" value="<?php echo $name;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp;
        <?php echo $name_error;?>
        </font></td>
	</tr>
	<tr>
		<td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="email">Email:  </label>
        </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="email" value="<?php echo $email;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp;
        <?php echo $email_error;?>
        </font></td>
	</tr>
	<tr>
	
		<td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="company">Company:  </label>
        </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="company" value="<?php echo $company;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp; 
        <?php echo $company_error;?>
        </font></td>
	<br />
	</tr>
	<tr>
		<td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="telephone">Telephone:  </label>
        </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="telephone" value="<?php echo $tel;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp;</font></td>
	</tr>
	<tr>
		<td width="117"> <div align="left"><font color="#000080" size="2" face="Verdana, Arial, Helvetica, sans-serif">Fax:
      </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="fax" value="<?php echo $fax;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp;</font></td>
	</tr>
	<tr>
		<td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="name">Subject:  </label>
        </font></div></td>
		<td width="176"><font size="2" color="#000080" face="Verdana">
		<input type="text" name="subject" value="<?php echo $subject;?>" size="20" /></font></td>
		<td><font color="#000080" size="2">&nbsp;
        <?php echo $subject_error;?>
		</font></td>
	</tr>
</table>
<p>
<select size="1" name="D1" style="color: #000080; font-family: Verdana; font-size: 10pt">
<option value="val">Sales</option>
<option value="liz"> Support </option>
<option value="david">Accounts </option>
<option value="liz">Further Information </option>
</select></p>
<p><font size="2">
<?php echo $comments_error;?>
<br />
<br />
<textarea name="comments" rows="10" cols="33"></textarea><br />
<br />
</font>
<input type="submit" name="submit" value="Send" style="font-family: Verdana; font-size: 10pt;"/>
</p>
</form>
</html>
 
I'm not sure where the error is. probably in the headers. i have redone some of the code in the search for an error. this works on my system (so far as i am able to discern)
Code:
<?php
//moved this function out of the master if statement
function CheckMail($email) { 
// this function checks if the email format is ok,
//if the chars are allowed, if there are enough chars in the TLD learn more
//about eregi function here: [URL unfurl="true"]http://www.php.net/manual/en/function.eregi.php[/URL]
	if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$",$email)){
		return true;
	} else 	{
		return false;
	}
}

if ( isset($_POST['submit'] )) { 	//changed to isset
	
	$valid = 8768;
	$name = $_POST['name'];

	if ( empty($name) ) {
		$valid = 3534;
		$error['name'] = 'You did not enter your name';
	}

	$company = trim($_POST['company']);	//changed to trim

	if ( empty($company) ) {
		$valid = 3534;
		$error['company'] = 'You did not enter your company';
	}

	//EMAIL VERIFICATION


	if (	(empty($_POST['email'])) 
			|| 
			(!CheckMail($_POST['email']))
		) {
	//here we check if the email is
	//empty and if the previous function controls are passed, if there are
	//errors the user is sent to a previously prepared custom error page
		$valid=3534;
		$error['email'] = 'Please enter a valid Email address';
	} else {
		$email = $_POST['email'];
	}

    $domains = array('@dodo','@hotmail', '@yahoo', '@gmail', '@aol','@TEE','@apat','@interfere','@inwind','@isiline','@izsmportici','@libero','@liero','@rai','@tin','@tinet','@tiscali');
	str_ireplace ( $domains, "###", $email, $cnt);
	if ($cnt > 0){
    	$valid = 3534;
		$error['email'] = 'Sorry, Internet Email accounts are not allowed';
	}
    

	$tel = trim($_POST['telephone']);
	$fax = trim($_POST['fax']);

	$subject = trim($_POST['subject']);

	if ( empty($subject) ) {
		$valid = 3534;
		$error['subject'] = 'You did not enter a subject';
	}

	$comments = trim($_POST['comments']);

	if ( empty($comments) ) {
		$valid = 3534;
		$error['comments'] = 'You did not enter any comments';
	}

	$dept = trim($_POST['D1']);
	$deptadd = "{$dept}@myserver.com.au";

	$message .= 'Name: ' . $name . "\n";
	$message .= 'Company: ' . $company . "\n";
	$message .= 'Email: ' . $email . "\n";
	$message .= 'Tel: ' . $tel . "\n";
	$message .= 'Fax: ' . $fax . "\n\n";
	$message .= 'Comments: ' . "\n";
	$message .= $comments . "\n";

	if ( $valid == 8768 ) {
		$sendto = $deptadd;
		$headers = 	"From: $email \r\n" . 
					'Reply-To: ' . $email . "\r\n\r\n";

		mail($sendto, $subject, $message, $headers);
		header ("Location: index.php?view=contact_thanks");
		exit();	//to stop the form from displaying
	} 
}//make the form into a function
?>

<html>
<form action="index.php?view=contact2" method="post">
<font size="2">
<label for="name">
<p></p><img border="0" src="image_folder1/contactus2.jpg" width="117" height="26"></label></font><p>&nbsp;</p>
<table border="0" width="100%" id="table1">
    <tr>
        <td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="name">Name:  </label>
        </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="name" value="<?php echo $name;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;
        <?php echo $error['name'];?>
        </font></td>
    </tr>
    <tr>
        <td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="email">Email:  </label>
        </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="email" value="<?php echo $email;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;
        <?php echo $error['email'];?>
        </font></td>
    </tr>
    <tr>
    
        <td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="company">Company:  </label>
        </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="company" value="<?php echo $company;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;
        <?php echo $error['company'];?>
        </font></td>
    <br />
    </tr>
    <tr>
        <td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="telephone">Telephone:  </label>
        </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="telephone" value="<?php echo $tel;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;</font></td>
    </tr>
    <tr>
        <td width="117"> <div align="left"><font color="#000080" size="2" face="Verdana, Arial, Helvetica, sans-serif">Fax:
      </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="fax" value="<?php echo $fax;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;</font></td>
    </tr>
    <tr>
        <td width="117"><div align="left"><font size="2" color="#000080" face="Verdana, Arial, Helvetica, sans-serif">
          <label for="name">Subject:  </label>
        </font></div></td>
        <td width="176"><font size="2" color="#000080" face="Verdana">
        <input type="text" name="subject" value="<?php echo $subject;?>" size="20" /></font></td>
        <td><font color="#000080" size="2">&nbsp;
        <?php echo $error['subject'];?>
        </font></td>
    </tr>
</table>
<p>
<select size="1" name="D1" style="color: #000080; font-family: Verdana; font-size: 10pt">
<option value="val">Sales</option>
<option value="liz"> Support </option>
<option value="david">Accounts </option>
<option value="liz">Further Information </option>
</select></p>
<p><font size="2">
<?php echo $error['comments'];?>
<br />
<br />
<textarea name="comments" rows="10" cols="33"></textarea><br />
<br />
</font>
<input type="submit" name="submit" value="Send" style="font-family: Verdana; font-size: 10pt;"/>
</p>
</form>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top