leggi il manuale ufficiale php
http://php.net/manual/en/function.header.php
dice:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
puoi inviare un header solo se prima non c'è stato alcun output a video. Se tu fai uno script php che contiene degli "a capo" fuori dai blocchi <?php e ?>, questi costituiscono un output (cioè vengono visualizzati a video). Esempio:
codice:
<?php
... codice php
?>
<?php
... codice php
header(".......");
?>
è sbagliato, perchè tra i due blocchi c'è una riga vuota e l'header viene invocato dopo che un output è già stato mandato alla pagina.
codice:
<?php
... codice php
?>
<?php
... codice php
header(".......");
?>
invece è corretto, perchè tra i due blocchi php non c'è nulla.
attenzione che
codice:
<?php
... codice php
?>
<?php
... codice php
echo "pippo";
header(".......");
?>
è sbagliato comunque, perchè "echo" produce un output.