Connecting Tech Pros Worldwide Forums | Help | Site Map

Cannot modify header information - headers already sent by

Newbie
 
Join Date: Jun 2007
Location: Melbourne, Australia
Posts: 30
#1: Mar 24 '08
I've been getting this error on my website since I first starting making up but I didn't know how to fix it, so I just ended up using HTML redirects to other pages instead of Header("Location:...").

Now I'm trying to retrieve uploaded files from my SQL database and going by the little tutorial from About.com the following file should download my uploaded file.

I get the header message because of "Header( "Content-type: $type");". I've read all the forums about this error but I can't seem to find the problem. As you can see there are no whitespaces, I'm not printing anything before I call the header.

However I am including two files which some say could cause the error but I don't know how?

[HTML]<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
include ('connection.php');
$id = $_GET['id'];

$query = "SELECT data, filetype FROM uploads WHERE id = " . $id;
$result = MYSQL_QUERY($query);
$data = MYSQL_RESULT($result,0,"data");
$type = MYSQL_RESULT($result,0,"filetype");
Header( "Content-type: $type");
print $data;

include ('close.php');
?>
<body>
</body>
</html>[/HTML]

Any help with this would be greatly appreciated.

Thanks

ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 24 '08

re: Cannot modify header information - headers already sent by


Following th session_start() and before the header statement, you have at least sent 5 HTML statements as output. See the following excerpt.

This is the text of an article by wildteen88, june 2006, who can explain this a lot better then I can. If you checked your code and still find no solution, show your code and we will have a look.
Quote:

Originally Posted by wildteen88

The reason why you are getting this message is because you have may have/be:

  • Whitespace before the opening php tag <?php
  • Outputting something to the browser before you use session_start, header, setcookie etc
session_start, setcookie, header and a few other functions write header information to the web server/browser what to do. Such as when you use session_start it requests the browser to create a cookie which stores the PHPSESSID.

If you have output before you use these functions then they are unable to send new header information as it has already been sent in the form text/html, and so you get the headers already sent error message.

You can easily find the source of the problem by looking at the error message. As the answer is actully in there, but most people dont notice it as they may not understand what the error means. So lets take this error message as an example:

Quote:
Warning: Cannot modify header information - headers already sent by (output started at C:\server\www\test.php:6) in C:\server\www\test.php on line 8
Now PHP has given us a clue here as it has told use where the output has started! Have look where it says output started at and it gives you the full path to the file and the line number. Now the line number that it stats is not usually where the output has started but where the output has ended, becuase the output could be above that line.
So lets look at test.php:
Expand|Select|Wrap|Line Numbers
  1.  1 <html>
  2.  2 <head>
  3.  3 <title>Header test</title>
  4.  4 </head>
  5.  5 <body>
  6.  6 <?php
  7.  7
  8.  8 header("Location: http://www.google.com");
  9.  9
  10. 10 ?>
  11. 11 </body>
  12. 12 </html>
As you can see line 6 is the opening PHP tag (< ?php) this isn't the output but look above that line you'll notice it has some HTML code. This html code is the cause of the error!

So when you get this error message again look for the clue where it says output started at and goto the file and line number it says. Look above the line number and find where your output is located to.

Hope that helps you understand why your are getting this error message. (Thanks to wildteen88)

Ronald
Newbie
 
Join Date: Jun 2007
Location: Melbourne, Australia
Posts: 30
#3: Mar 24 '08

re: Cannot modify header information - headers already sent by


That was perfect mate. Thanks a lot for your help, fixed all my problems.
Reply