PHP警告:无法修改标题信息-标题已由发送

PHP警告:无法修改标题信息-标题已由发送,php,apache,redirect,warnings,Php,Apache,Redirect,Warnings,我知道这个问题已经被问过无数次了,但我似乎找不到一个对我的问题有帮助的答案 我刚刚创建了一个新的php网站,正如您在标题中看到的,它一直在说: 警告:无法修改标题信息-标题已由第4行的/customers/e/7/8/andersws.dk/httpd.www/template/index.php:2)中的/customers/e/7/8/andersws.dk/httpd.www/template/index.php发送 到目前为止,文件中包含的内容包括: <!DOCTYPE html&

我知道这个问题已经被问过无数次了,但我似乎找不到一个对我的问题有帮助的答案

我刚刚创建了一个新的php网站,正如您在标题中看到的,它一直在说:

警告:无法修改标题信息-标题已由第4行的/customers/e/7/8/andersws.dk/httpd.www/template/index.php:2)中的/customers/e/7/8/andersws.dk/httpd.www/template/index.php发送

到目前为止,文件中包含的内容包括:

<!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>


所以我真的看不出我做错了什么。

这是因为您在标题之前指定了doctype。发送有效负载后,不能输出标头。因此,将其更改为:

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

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

    </body>
</html>

HTTP头只能在正文之前发送。页面的所有输出都属于正文。在您的示例中,您试图在
节点的输出之后输出标题

使用以下命令:

<?
    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>

使用这个

<?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(); ?>


有关
`ob_start()

的更多详细信息,请参阅,这很可能是因为在您的开头
顶部有一行
“请使用它:”---为什么?;-)“用这个:”---为什么?;-)——有一个实际的原因,用户说,这类问题有一百万个问题。所以,我想OP已经知道问题的根本原因了。错别字问题,我没有说明我们坚持50%对50%错的理由。我认为最有可能的原因是OP在PHP之上声明了一个doctype,它在头之前作为输出。发生这种情况的原因太多了,但这就是我的看法