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

我纠结于这个错误已经有一段时间了。

首先,我只是认为它是空白,但经过进一步的研究,我认为它可能是一个类似的问题:

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

我猜测 include 头部和 header ()一起导致了这个问题,但是我不确定如何重新排列代码来消除这个错误。

如何删除错误?

<?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];
}
}
?>
411140 次浏览

Check something with echo, print() or printr() in the include file, 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.

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

  • 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 Location header, you should form an absolute URL like http://www.mysite.com/some/path.php

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

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

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.

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.

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

output_buffering = On

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

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