[PHP] Headers already send out

Developer
Joined
Jul 28, 2009
Messages
983
Reaction score
133
Hey,

I'm still working on my api, but i am getting an error saying headers already sent out.
And i can't find the problem, could someone help me out?

Code:

<?php
setcookie("diaxavote","",time()+(3600*12),"/");
?>

<html>
<form action="http://votingapi.com/vote.php?username=diaxahotel26" method="post" name="votingform">
<input type="hidden" name="api_url" value="http://diaxa.eu/index" />
</form>
<script language="javascript">setTimeout("document.forms[\'votingform\'].submit()",0); </script>
</html>
 
Last edited:
Are you sure there aren't any white spaces before the PHP tags in your script?
PHP: setcookie - Manual

PHP said:
Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
 
Are you sure there aren't any white spaces before the PHP tags in your script?
PHP: setcookie - Manual


This is my hole script/page:

<?php
setcookie("diaxavote","",time()+(3600*12),"/");
?>

<html>
<form action="http://votingapi.com/vote.php?username=diaxahotel26" method="post" name="votingform">
<input type="hidden" name="api_url" value="http://diaxa.eu/index" />
</form>
<script language="javascript">setTimeout("document.forms[\'votingform\'].submit()",0); </script>
</html>
 
Headers already sent out on line 1, something like that.
I restored my old files, so i can't get the actual error back.
 
It always has something to do with setcookie or session_start(); . Make sure their literally on the very first line in all documents especially if some documents are being required into others.
 
Lol? I never said it did require it?
I just said make sure either if hes using setcookie OR session_start() they should be on the first line.
or header()...

Use this technique to help find this error in PHP:
- put this code on the file BEFORE the code causing the error:
PHP:
// this is the code:
if (headers_sent()) {
    die('headers sent before this.');
}
// example of error-causing code:
set_cookie(..);
- The code will exit the script if headers are already sent.
- View the source of the web-page, and if you see nothing but the text, highlight the entire file.
- Anything before the text "headers sent before this" is the content causing the error.
- If there is a space before the text, then look for a space in the file before '<?php'.
- If there is a line break before the text, look for a line break before '<?php'.
- Remove the problem content.

This is one reason PHP is a gigantic anti-pattern. Note that if you are using multiple PHP files, you must look for space after the '?>' tag as well. Most text editors I use will add an extra line to the end of the file when you save. It is good practice to have a trailing blank line after your programs.. Makes life easier for "future programmer" who edits the file. In PHP, it is good practice to never use the '?>' closing tag. The only reason to use that tag is if you are including HTML or text inside your PHP file, which is bad practice. It is much better to code PHP never relying on the '?>' tag, as it causes so many problems. Furthermore, if you are using multiple PHP files, finding a single space among 20 class files is very time consuming. It is almost worth running a multi-file search for ' <?php' - notice the prefixing space. If it weren't for the popularity, and wide support from shared web hosts, there would be no reason to program in such a hostile environment. So, if you don't have to code in PHP, then I highly recommend an alternative.
 
Last edited:
Can also be caused by BOM. This happens if you save the PHP file with the wrong encoding.

utf 8 - UTF-8 BOM signature in PHP files - Stack Overflow

What he said, getting headers already sent on first line, when u can obviously see that there is nothing in first line ( expect of what you've written ) is in 99% cases the BOM ( Byte Of Mark ) and the solutions has been also written, notepad++ -> encoding -> convert to utf-8 without BOM, or what ever encoding You use.

PHP files does not have to contain only ASCII characters, how else you would use native signs like: ąężźćł etc.

I do use utf-8 without BOM for all my code, works like a charm.
 
PHP files does not have to contain only ASCII characters, how else you would use native signs like: ąężźćł etc.

I do use utf-8 without BOM for all my code, works like a charm.
PHP UTF-8 cheatsheet - blog.loftdigital.com
In addition to that step, you should follow the 9 steps noted in the above document- and refrain from using any libraries containing html_entity or html_entities_decode, or html_special_chars or w/e, and lots of other circumstances to worry about.... Like, I believe str_len and many str_ functions

Or, if you aren't supporting multi-byte data, you can just use ASCII for all your data and the data will work with UTF-8 and with PHP and with MySQL and with crappy SQL escape algorithms as they should.

Don't get me wrong, I feel everything should be UTF-8 compatible. Unfortunately, PHP functions are mostly C wrappers so mostly work as unexpected only with ASCII characters. My solution is to use UTF-8 encoding with JavaScript files... and.. I don't use PHP.

I prefer my programs work as I expect them to work- not programs that act like they support UTF-8 yet some parts only work with ASCII data and the data passed to those functions (may/may not- no way to tell via program) contain multi-byte UTF-8 characters which will cause bugs and loop-holes or security risks in regex or C, and lots of ugly possibilities.

Note: What you can do in PHP to detect incompatible encoding is compare the utf8_decode() result with the data- it is a very annoying process, and if you choose to allow multi-byte data, you are obliged to know the risks of these functions and are expected to code your own replacements when needed- not using any of these ASCII-dependent features:
http://www.pwsdb.com/pgm/?p=64
 
Last edited:
Back