php - 什么可能导致标题已经发送,我将如何追踪它?

标签 php header http-headers

首先,我对 PHP 或发送 header 绝不陌生。但是,我收到以下错误:

Warning: Cannot modify header information - headers already sent in /var/www/oraviewer/download/scripts/start.inc on line 30

Warning: Cannot modify header information - headers already sent by (output started at /var/www/oraviewer/download/scripts/start.inc:30) in /var/www/oraviewer/download/scripts/start.inc on line 31

Warning: Cannot modify header information - headers already sent by (output started at /var/www/oraviewer/download/scripts/start.inc:30) in /var/www/oraviewer/download/scripts/start.inc on line 32

3 个错误,因为我正在尝试发送 3 个 header 。

我已经搜索了包含的每个文件,检查了它们是否有任何可能发送的标题、换行符、文本等,但没有。

我已经习惯了这种错误告诉你打印的第一位来自哪里(请参阅第二个 2 个错误以了解我的意思)但第一个什么也没告诉我。

据我所知,自从我上次让它们工作以来,这些文件都没有改变,并且使用备份副本,它们工作正常。不幸的是,如果我用备份文件替换它们,问题仍然存在。我会发布文件,但大约有六个文件,总计几百行。

那么,我的主要问题是:可能是什么原因造成的,我该如何追踪它的来源?

编辑:30-32 处的 3 行,这是正在打印的特定标题

header('Content-Type: application/octet-stream');
header('Content="text/plain;charset=ISO-8859-1"');
header('Content-Disposition: attachment; filename="'.$fileName.'"');

如果我注释掉一个,错误会从下一个开始,所以我知道它在这些之前。

getExtract.php文件中start.inc之前的代码:

<?php
include_once "start.inc";

start.inc 文件:

<?php
$extract = $_GET['extract'];
$debug = $_GET['debug'];
include "../../lib/conf.inc";
include $mainsitedir."/lib/db_connect.inc";
include $mainsitedir."/lib/utils.inc";
include "../queries/get_output_file_name.php";

$db_link = connect_db();
$query = "SELECT * FROM extract_wxo_list WHERE extract_id='{$_GET['extract']}'";
$file = db_get_row(db_query($db_link, $query));
$fileName = $file['OUTPUT_FILE_NAME'];

if (!empty($_GET['versiondate'])) {
    $sql = "SELECT * FROM version_history WHERE version_start_date = '{$_GET['versiondate']}'";

    $vquery = db_query( $db_link, $sql );
    if ($vrow = db_get_row($vquery)) {
        $splitName = preg_split("/[.]/", $fileName, 2);
        $fileName = $splitName[0].'_'.str_replace(".", "", $vrow['VERSION']).'.'.$splitName[1];
    }
}

if ( $_GET['debug'] ) {
    echo "$fileName<br /><pre>";
} else {
    header('Content-Type: application/octet-stream');
    header('Content="text/plain;charset=ISO-8859-1"');
    header('Content-Disposition: attachment; filename="'.$fileName.'"');
}
date_default_timezone_set('UTC');
?>

靠近顶部的 4 个包含在另外几个文件中是相同的。他们还以类似但不那么通用的方式使用 header ,因此我可以排除它们的原因。

最佳答案

检查您是否在 <?php 之前留下了空格 或者尝试使用 ob_start();和 ob_end_flush();

<?php
ob_start(); //Starts output puffer
echo "texttest";
if($bala==1) {
header("location:http://www.testdomain.org");
}
ob_end_flush(); //ends output puffer
?>

关于php - 什么可能导致标题已经发送,我将如何追踪它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2366079/

相关文章:

javascript - 计算网络 map 应用程序中栅格的平均值

php - WooCommerce选择性地显示缺货的产品

javascript - 移动标题不可点击

asp.net - 我可以在我的 html 文件中放置什么元标记,以便它自动在 Microsoft Word 中打开该页面?

python - 检查请求是否是 Python 中的 AJAX

php - CakePHP:无法按多个条件排序

php - 匹配 mysql 表中的重复电子邮件条目(标准格式)

c++ - 未在此范围内声明

javascript - Uncaught ReferenceError : handleLocationError is not defined google maps api

http-headers - 在 http 请求中设置 UserAgent