警告:无法修改标头信息 - 标头已由 ERROR 发送 [英] Warning: Cannot modify header information - headers already sent by ERROR

查看:22
本文介绍了警告:无法修改标头信息 - 标头已由 ERROR 发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为这个错误苦苦挣扎.

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:

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

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.

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

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.

如何消除错误?

<?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];
        }
    }
?>

推荐答案

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

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.

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

The very quick fix for your problem will be to add

ob_start();

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

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.

这将打开 PHP 的输出缓冲功能.在 PHP 中,当您输出某些内容(执行回显或打印)时,它必须在那时发送 HTTP 标头.如果您打开输出缓冲,您可以在脚本中输出,但 PHP 在刷新缓冲区之前不必发送标头.如果您打开它并且不关闭它,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.ini 配置文件,您可以找到并更改或添加以下内容

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

output_buffering = On

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

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

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

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

这篇关于警告:无法修改标头信息 - 标头已由 ERROR 发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆