php 警告:无法修改标头信息 - 标头已由 ERROR 发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9707693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:28:10  来源:igfitidea点击:

Warning: Cannot modify header information - headers already sent by ERROR

phpheader

提问by Rob

I've been struggling with this error for a while now.

一段时间以来,我一直在为这个错误而苦苦挣扎。

To start with, I just thought it was white space, but after further research I think it might be a problem similar to this:

一开始,我只是认为它是空白,但经过进一步研究,我认为这可能是一个类似于以下的问题:

Look for any statements that could send output to the user before this header statement. If you find one or more, change your code to move the header statement before them. Complex conditional statements may complicate the issue, but they may also help solve the problem. Consider a conditional expression at the top of the PHP script that determines the header value as early as possible and sets it there.

在此标头语句之前查找可以向用户发送输出的任何语句。如果找到一个或多个,请更改代码以将标题语句移到它们之前。复杂的条件语句可能会使问题复杂化,但它们也可能有助于解决问题。考虑 PHP 脚本顶部的条件表达式,它尽可能早地确定标头值并将其设置在那里。

I'm guessing the include header is causing the problem along with the header(), but I'm not sure how to rearrange the code to get rid of this error.

我猜测包含标头与标头()一起导致了问题,但我不确定如何重新排列代码以消除此错误。

How do I remove the error?

如何消除错误?

<?php
    $username = $password = $token = $fName = "";

    include_once 'header.php';

    if (isset($_POST['username']) && isset($_POST['password']))
        $username = sanitizeString($_POST['username']);

    $password = sanitizeString($_POST['password']); //Set temporary username and password variables
    $token    = md5("$password"); //Encrypt temporary password

    if ($username != 'admin')
    {
        header("Location:summary.php");
    }
    elseif($username == 'admin')
    {
        header("Location:admin.php");
    }
    elseif($username == '')
    {
        header("Location:index.php");
    }
    else
        die ("<body><div class='container'><p class='error'>Invalid username or password.</p></div></body>");

    if ($username == "" || $token == "")
    {
        echo "<body><div class='container'><p class='error'>Please enter your username and password</p></div></body>";
    }
    else
    {
        $query = "SELECT * FROM members WHERE username='$username'AND password = '$token'"; //Look in table for username entered
        $result = mysql_query($query);
        if (!$result)
            die ("Database access failed: " . mysql_error());
        elseif (mysql_num_rows($result) > 0)
        {
            $row = mysql_fetch_row($result);
            $_SESSION['username'] = $username; //Set session variables
            $_SESSION['password'] = $token;

            $fName = $row[0];
        }
    }
?>

回答by SamHennessy

The long-term answer is that all output from your PHP scripts should be buffered in variables. This includes headers and body output. Then at the end of your scripts do any output you need.

长期的答案是 PHP 脚本的所有输出都应该在变量中缓冲。这包括标题和正文输出。然后在脚本的末尾执行您需要的任何输出。

The very quick fix for your problem will be to add

解决您的问题的非常快速的方法是添加

ob_start();

as the very first thing in your script, if you only need it in this one script. If you need it in all your scripts add it as the very first thing in your header.php file.

作为脚本中的第一件事,如果您只在这个脚本中需要它。如果您在所有脚本中都需要它,请将其添加为 header.php 文件中的第一件事。

This turns on PHP's output buffering feature. In PHP when you output something (do an echo or print) it has to send the HTTP headers at that time. If you turn on output buffering you can output in the script but PHP doesn't have to send the headers until the buffer is flushed. If you turn it on and don't turn it off PHP will automatically flush everything in the buffer after the script finishes running. There really is no harm in just turning it on in almost all cases and could give you a small performance increase under some configurations.

这将打开 PHP 的输出缓冲功能。在 PHP 中,当您输出某些内容(执行回显或打印)时,它必须在那时发送 HTTP 标头。如果您打开输出缓冲,您可以在脚本中输出,但 PHP 不必在缓冲区刷新之前发送标头。如果您打开它而不关闭它,PHP 将在脚本完成运行后自动刷新缓冲区中的所有内容。在几乎所有情况下都打开它确实没有害处,并且可以在某些配置下为您带来小的性能提升。

If you have access to change your php.ini configuration file you can find and change or add the following

如果您有权更改 php.ini 配置文件,您可以找到并更改或添加以下内容

output_buffering = On

This will turn output buffering out without the need to call ob_start().

这将关闭输出缓冲,而无需调用 ob_start()。

To find out more about output buffering check out http://php.net/manual/en/book.outcontrol.php

要了解有关输出缓冲的更多信息,请查看http://php.net/manual/en/book.outcontrol.php

回答by Saiyam Patel

Check something with echo, print()or printr()in the include file, header.php.

检查的东西echoprint()printr()在包括文件,header.php

It might be that this is the problem OR if any MVC file, then check the number of spaces after ?>. This could also make a problem.

这可能是问题所在,或者如果有任何 MVC 文件,请检查?>. 这也可能造成问题。

回答by Pierre-Olivier

You are trying to send headers information after outputing content.

您尝试在输出内容后发送标头信息。

If you want to do this, look for output buffering.

如果您想这样做,请寻找输出缓冲。

Therefore, look to use ob_start();

因此,寻找使用 ob_start();

回答by James C

There are some problems with your header()calls, one of which might be causing problems

您的header()通话存在一些问题,其中之一可能会导致问题

  • You should put an exit()after each of the header("Location:calls otherwise code execution will continue
  • You should have a space after the :so it reads "Location: http://foo"
  • It's not valid to use a relative URL in a Locationheader, you should form an absolute URL like http://www.mysite.com/some/path.php
  • 您应该exit()在每个header("Location:调用之后放置一个,否则代码执行将继续
  • 你应该在后面有一个空格:所以它读"Location: http://foo"
  • Location标题中使用相对 URL 是无效的,您应该形成一个绝对 URL,如http://www.mysite.com/some/path.php