Warning: Cannot modify header information

Hello everyone.

I've been getting the following error message when attempting to log in on a site I am working on:

Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/np/index.php:8) in /home/mysite/public_html/np/login.php on line 19

I have researched the message, and it appears that it usually results from having a space or end of line character at the end of any php closing tags. I've checked my code many times and I can't seem to find any problems. Perhaps someone could tell me what I am doing wrong.

<html>
<head>
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" >
<tr valign="top">
<td width="15%"><table cellspacing="0" cellpadding="5"><tr valign="top"><td class="style1"><?php include("login.php"); ?></td></tr></table></td>
<td width="85%"><table cellspacing="0" cellpadding="5"><tr valign="top"><td><?php include("register.php"); ?></td></tr></table></td>
</tr>
</table>
</body>
</html>

The error occurs when I attempt to set a cookie in my login.php file. Whenever I log in directly from the login.php page, everything works perfectly. However, any time that I attempt to login through the index.php page I get the above error. I appreciate any help in advance!
[1655 byte] By [SRD] at [2007-11-20 7:46:58]
# 1 Re: Warning: Cannot modify header information
The header() function cannot be called after some output has already been set. For example...

Correct method.
<?php
header('whatever');
?>
<html>
Incorrect method.
<?php
echo 'whatever';
header('whatever');
?>
PeejAvery at 2007-11-10 3:56:33 >
# 2 Re: Warning: Cannot modify header information
So is any html before the header function considered output? How would I include my login form within a table without invoking an error?
SRD at 2007-11-10 3:57:31 >
# 3 Re: Warning: Cannot modify header information
Anything that is written to the client-side is called output. This includes html tags, echoing, and anything outside of PHP tags that writes to the client-side.
PeejAvery at 2007-11-10 3:58:29 >
# 4 Re: Warning: Cannot modify header information
So, to answer my question, is it possible to include the login.php file within a table? Might you suggest a way around this? My first idea was to have the login page redirect you back to the index page after the form had been submitted, and the cookie had been set. However, this would make it difficult to display errors, etc. without passing a variable through the url (i.e. index.php?badlogin=true). I suppose this is an option, but I wasn't sure if there was any other way.

Am I on the right track with this?
SRD at 2007-11-10 3:59:34 >
# 5 Re: Warning: Cannot modify header information
You can do this but put the login check at the top of the page.

<?php
if(!isset($_COOKIE['whatever'])){header('Location: index.php?badlogin=true');}
?>
<html>
...
Else, you will have to use JavaScript to redirect.
PeejAvery at 2007-11-10 4:00:32 >
# 6 Re: Warning: Cannot modify header information
Thanks, I got it working.
SRD at 2007-11-10 4:01:31 >
# 7 Re: Warning: Cannot modify header information
Glad to hear it! :wave:
PeejAvery at 2007-11-10 4:02:31 >