This is one of the most common errors. You often get it when your using session_start() or header().
Headers are sent automatically when any output is sent to the user. You send output with print/echo (and some other functions) as well as having anything outside php tags. This includes blank lines spaces, tabs and all other characters. You must send the header BEFORE the output. You must watch out not to have any blank lines before the <?php tag and after the ?> tag
PHP Code:
<?php // There should be no spaces before tag.
session_start();
?>
// A new line which shouldn't be here. This is important especially the file is included to another file.
Coding in such a way might be difficult, so you might ask if there is a way to wait with sending the output until the end of script execution. This is where output buffering (ob for short) comes in to play. If you start ob it will save all output and headers into memory instead of sending it. You can send all output at once with the headers. phpsu
PHP Code:
<?php
ob_start(); // Send the output.
?>
<html>
<head>
<title>OB test</title>
</head>
<body>
<?php
session_start();
print SID;
?>
</body>
</html>
<?php
header('Content-type: text/plain'); // An example header.
ob_end_flush(); // Send the output.
?>
You can try commenting out the ob_start();
Functions used:
ob_start()
ob_end_flush()
-mentioned but not OB functions
session_start()
header()
Good to read:
output buffering
If you spot an error (or have a suggestion) PM me. I'll fix it as fast as I can.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.
TITLE:How to resolve the 'headers already sent' error?
Copyright 2008 The PHPsu
All rights reserved.
This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:56:39 GMT-8 2008
Last updated: Fri Jun 6 22:56:39 GMT-8 2008