-1

I know this has been asked a million times already but I can't seem to find an answer that helps with mine.

I just started a new php website and as you saw in the title it keeps saying:

Warning: Cannot modify header information - headers already sent by (output started at /customers/e/7/8/andersws.dk/httpd.www/template/index.php:2) in /customers/e/7/8/andersws.dk/httpd.www/template/index.php on line 4

All that is in the file so far is:

<!DOCTYPE html>
<?
    if(file_exists('first.run')){
        header('location: index.php');
    }
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>
    </body>
</html>

So I really can't see what I'm doing wrong.

Sam
  • 7,252
  • 16
  • 46
  • 65
  • 1
    It's most likely because of this line ` ` on top of your opening `` that counts as output before header. Place it above `` --- This type of question has been asked thousands of times; yes, thousands. – Funk Forty Niner Apr 06 '14 at 15:25

3 Answers3

4

It's because you specified a doctype before the header. You cannot output a header after the payload has been sent. So change it to:

<?
    if(file_exists('first.run')){
        header('location: index.php');
    }
?>
<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>

    </body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
smistry
  • 1,127
  • 1
  • 9
  • 9
2

The HTTP headers can only being sent before the body. All output of the page belongs to the body. In your example you are attempting to output the header after the output of the <doctype> node.

Use this:

<?
    if(file_exists('first.run')){
        header('location: index.php');
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>
    </body>
</html>
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • *"Use this:"* --- why? ;-) [`lol`](http://stackoverflow.com/questions/22895909/php-warning-cannot-modify-header-information-headers-already-sent-by#comment34940647_22895909) – Funk Forty Niner Apr 06 '14 at 15:26
0

Use this;

<?php
ob_start();
if(file_exists('first.run')){
    header('location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>
    </body>
</html>
<?php ob_end_flush(); ?>

See here for more detail about `ob_start()

Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • *"Use this:"* --- why? ;-) [`Most likely answer`](http://stackoverflow.com/questions/22895909/php-warning-cannot-modify-header-information-headers-already-sent-by#comment34940647_22895909) --- There is an actual reason. – Funk Forty Niner Apr 06 '14 at 15:30
  • User said that, there is a million question asked about this kind of question. So, I assume, OP already knows basic root cause of problem. Typo issue, I didn't state the reason – Hüseyin BABAL Apr 06 '14 at 15:34
  • We stand at being 50% right and 50% wrong. What I think is the most likely reason is that the OP is declaring a doctype on top of PHP, which counts as output before header. There are far too many reasons why this is happening, but that's what my take on it is. ;-) – Funk Forty Niner Apr 06 '14 at 15:38