View Full Version : Cannot modify header information ...
ajthornton
11-29-2005, 04:36 PM
HELP!!!
I am new to php. http://linux.thorncom.net is my first attempt at creating pages using php. I know a great deal about html, but I want to do more than html can do. I am getting the following message when I process mu contact form. Warning: Cannot modify header information - headers already sent by (output started at /home/linux/public_html/header-file.inc:6) in /home/linux/public_html/contact.php on line 60 I do not know why this is happening. It was working fine one day and the next it started giving me this error.
Can anyone help me. If you tell me what information you need. I will either post it or send it to you. Just let me know.
Thanks in advance,
ajthornton:confused:
NancyJ
11-29-2005, 05:03 PM
well you dont post your code so theres nothing we can really do.
BUT I'm assuming that you're trying to use a header("Location: file.php") your header.inc is outputting to the browser before your redirect
ajthornton
11-29-2005, 05:21 PM
I believe the problem is suppose to be here.
if ($name && $email && $linuxDistro)
{
// send an email
$body = "This is a posting from, {$_POST['name']}.\n\n{$_POST['name']} is using '{$_POST['linuxDistro']}'.\n\nThese comments were left:\n'{$_POST['comments']}'";
$from = 'From: ajthornton@thorncom.net\r\n';
mail ('ajthornton@thorncom.net', 'Posting from linux.thorncom.net', $body, $from);
header ('Location: http://localhost/thorncom/thankyou.php');
exit ();
}
bcarl314
11-29-2005, 05:39 PM
Right.
The problem is most likely that you're printing something (or echoing something) before you get to that code. OR, it could be that you have a space / line break at the top of the file OR a space / line break at the end of an include file that's included before the code you posted.
ajthornton
11-29-2005, 05:51 PM
Here is the whole file.
<?php
$page_title = 'linux.thorncom.net | contact form';
include ('header-file.inc'); // Use the HTML header file
if (isset($_POST['submit']))
{
$message = NULL;
// check for name
if (strlen($_POST['name']) > 0)
{
$name = TRUE;
}
else
{
$name = FALSE;
$message .= '<p>You forgot to enter your name!</p>';
}
// check for email address
if (strlen($_POST['email']) > 0)
{
$email = TRUE;
}
else
{
$email = FALSE;
$message .= '<p>You forgot to enter your email address!</p>';
}
// check for linuxDistro
if (strlen($_POST['linuxDistro']) > 0)
{
$linuxDistro = TRUE;
}
else
{
$linuxDistro = FALSE;
$message .= '<p>You forgot to enter your Linux Distribution!</p>';
}
// check for comments
if (strlen($_POST['comments']) > 0)
{
$comments = TRUE;
}
else
{
$comments = FALSE;
$message .= '<p>You forgot to enter your Comments!</p>';
}
// if everything checks out ok
if ($name && $email && $linuxDistro)
{
// send an email
$body = "This is a posting from, {$_POST['name']}.\n\n{$_POST['name']} is using '{$_POST['linuxDistro']}'.\n\nThese comments were left:\n'{$_POST['comments']}'";
$from = 'From: ajthornton@thorncom.net\r\n';
mail ('ajthornton@thorncom.net', 'Posting from linux.thorncom.net', $body, $from);
header ('Location: http://localhost/thorncom/thankyou.php');
exit ();
}
else
{
$message .= '<p>Please try again!</p>';
}
}
// print error message if there is one
if (isset($message))
{
echo '<font color="red">', $message, '</font>';
}
?>
<font size="+2">linux.thorncom.net | Contact Form</font><br><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="50%" border="0" cellpadding="1" cellpadding="0">
<tr>
<td align="right" valign="top"><b>Name:</b></td>
<td><input type="text" name="name" size="20" maxlength="40" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Email Address:</b></td>
<td><input type="text" name="email" size="40" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Linux Distribution:</b></td>
<td><input type="text" name="linuxDistro" size="30" maxlength="50" value="<?php if (isset($_POST['linuxDistro'])) echo $_POST['linuxDistro']; ?>" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Comments:</b></td>
<td><textarea name="comments" rows="4" cols="50" /><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input type="submit" name="submit" value="Submit Information" /> <input type="reset" name="reset" value="Reset Form" /></div></td>
</tr>
</table>
<font color="red" size="-1">All fields required!</font>
</form>
<!-- end of form -->
<?php
include ('footer.inc'); // Use the HTML footer file.
?>
ralph l mayo
11-29-2005, 06:39 PM
Warning: Cannot modify header information - headers already sent by (output started at /home/linux/public_html/header-file.inc:6) in /home/linux/public_html/contact.php on line 60
The problem is happening at line six of header-file.inc. Post that one.
edit never mind this is it:
(edit never mind again edited out :))
You can't send anything to the browser before you send the headers.
Move :
include ('header-file.inc'); // Use the HTML header file
from the first file to the very end of the first PHP block, just before the rest of the HTML
ajthornton
11-29-2005, 06:51 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title><?php echo $page_title; ?></title>
</head>
<!--
Author: A. James Thornton
Date: Nov 16, 2005
Filename: header-file.inc
-->
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" link="#000000" alink="#000000" vlink="#000000" bgcolor="#000000">
<table border="0" cellspacing="0" cellpadding="4">
<!-- TOP ROW -->
<tr>
<td rowspan="2" bgcolor="#000000"><img src="images/linux-thorncom-logo.gif" width="168" height="94"> </td>
<td width="100%" bgcolor="#000000"><font color="#FFFFFF" size="+2" face="Courier New, Courier, mono"><strong>linux.thorncom.net</strong></font></td>
<td width="10" rowspan="2" bgcolor="#000000"> </td>
</tr>
<!-- NAVIGATIONAL ROW -->
<tr>
<td bgcolor="#8D0000">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td width="20%" align="center" valign="center" bgcolor="#8B9798"><a STYLE="text-decoration:none" href="index.php">Home</a></td>
<td width="20%" align="center" valign="center" bgcolor="#8B9798"><a STYLE="text-decoration:none" href="contact.php">Contact</a></td>
<td width="20%" align="center" valign="center" bgcolor="#8B9798"><a STYLE="text-decoration:none" href=""></a></td>
<td width="20%" align="center" valign="center" bgcolor="#8B9798" nowrap="nowrap"><a STYLE="text-decoration:none" href=""></a></td>
<td width="20%" align="center" valign="center" bgcolor="#8B9798" nowrap="nowrap"><a STYLE="text-decoration:none" href=""></a></td>
</tr>
</table>
</td>
</tr>
<!-- CONTENT ROW -->
<tr>
<td bgcolor="#000000"> </td>
<td bgcolor="#FFFFFF">
<!-- PAGE SPECIFIC CONTENT STARTS HERE -->
whizard
11-29-2005, 07:27 PM
You cant use a header redirect after you have output data to the browser. You might be able to do this better by having your form and content seperated in two different files. When the user clicks on the contact link, he will get the form. Then, the form submits to a seperate PHP file which checks to make sure all the inputs are filled and then either sends back an error through the URL (formfile.php?errormsg=1) or mails the info and redirects to the original page, or somewhere else (formfile.php?result="success")
HTH
Dan
marek_mar
11-29-2005, 08:12 PM
FAQ (http://www.codingforums.com/showpost.php?p=300877&postcount=6)...
Velox Letum
11-30-2005, 12:32 AM
As marek posted, the FAQ has the method to use an output buffer to place around the content, which'll shift the header correctly. You could alternatively use a meta redirect.
ajthornton
12-01-2005, 03:10 PM
I have my form fixed. I decided to use a separate html form and php form handle. If anyone is interested the link is: linux.thorncom.net/contact.html (http://linux.thorncom.net/contact.html)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.