简体   繁体   English

为什么收到警告:无法修改标头信息-标头已经发送过(输出从...开始)?

[英]Why am I getting Warning: Cannot modify header information - headers already sent by (output started at …)?

I'm reusing a search PHP script, but I get this error: 我正在重用搜索PHP脚本,但出现此错误:

Warning: Cannot modify header information - headers already sent by (output started at /Volumes/Work/MAMP/PC Flag/php_scripts/conection.php:2) in /Volumes/Work/MAMP/PC Flag/includes/search.php on line 63 警告:无法修改标头信息-已在/ Volumes / Work / MAMP / PC Flag / includes / search.php中发送的标头(输出从/ Volumes / Work / MAMP / PC Flag / php_scripts / conection.php:2开始) 63行

line #2 conection.php: 第2行corction.php:

$con = mysqli_connect("localhost", "root", "root", "cms");

line #63 search.php: 第63行search.php:

header('Location: search-page.php?keywords=' . urlencode($keywords));

What is the problem? 问题是什么?

Also I need to mention that the script doesn't redirect me anymore to search-page.php as it should. 我还需要提到的是,该脚本不再像应有的那样将我重定向到search-page.php。

When you use header() it shouldn't be any output before, not even a space, you have two solution: 当您使用header()它之前不应有任何输出,甚至也不能有空格,您有两种解决方案:

Dirty one 肮脏的一

Use ob_clean() before the header to clean the output buffer 在标头之前使用ob_clean()清理输出缓冲区

ob_clean();
header('Location: search-page.php?keywords=' . urlencode($keywords));

Correct one 纠正一个

Search in your script and your request route for any output, content before <?php , echo es, HTML code, print s or spaces after the ?> are usually the cause of this ;) 在脚本和请求路径中搜索任何输出, <?php之前的内容, echo es,HTML代码, ?>之后的print?>后的空格通常是造成此问题的原因;)

Debug 除错

This snippet may help you to find out where is the output in your code: 该代码段可以帮助您找出代码输出的位置:

var_dump(ob_get_contents());die()

Put this before line 63 将此放在第63行之前

Javascript workaround JavaScript解决方法

In the case that everything fails, you have another option, use javascript to redirect, although I recommend you to keep trying without get to this point :) 万一一切失败,您可以使用javascript进行重定向,尽管我建议您继续尝试,但请注意:)

Replace the header(...) with this: header(...)替换为:

echo '<script>window.location.href="search-page.php?keywords=' . urlencode($keywords)) . '";</script>';

You have send a header already as error states. 您已经发送了标头作为错误状态。 Please investigate when and how headers are sent . 请调查何时以及如何发送标头

That usually happens when something is printed before that line to output buffer. 当在该行之前的某些内容打印到输出缓冲区时,通常会发生这种情况。

Make sure that you are using UTF-8 without BOM document encoding - BOM sign is invisible in most text editors yet can be interpreted as content which forces sending HTTP headers. 确保您使用的是UTF-8 without BOM文档编码的UTF-8 without BOM -BOM符号在大多数文本编辑器中不可见,但可以解释为强制发送HTTP标头的内容。

You can debug your output with Output Control function 您可以使用输出控制功能调试输出

It's happening because output has been sent already. 之所以这样,是因为已经发送了输出。 You can't edit Header Information when you have echo'd anything! 回显任何内容后都无法编辑标题信息!

It seems like the error you're getting is because an output have been occured.you can fix it with this code : 您似乎收到的错误是因为发生了输出。您可以使用以下代码对其进行修复:

if (headers_sent()) {
    ob_clean();
}  

Just found this detailed answer . 刚找到这个详细答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我没有收到“警告:无法修改标头信息-标头已经发送”的信息,我希望得到它? - Why I'm not getting a 'Warning: Cannot modify header information - headers already sent by' as I expected to get one? PHP - 无法修改标头信息 - 标头已由(输出开始于 - PHP - cannot modify header information - headers already sent by (output started at 在CSV下载期间-警告:无法修改标头信息-已发送的标头(输出始于 - During CSV downloading - Warning: Cannot modify header information - headers already sent by (output started at PHP 重现警告“无法修改 header 信息 - 标头已由(输出开始于...”与 XAMPP 8..1.1 - PHP reproduce warning "Cannot modify header information - headers already sent by (output started at... " with XAMPP 8..1.1 警告:无法修改已发送的标头信息标头(输出始于(/themes/kaboodle/functions/admin-setup.php - Warning:Cannot modify header information-headers already sent by (output started at (/themes/kaboodle/functions/admin-setup.php 如何删除警告无法修改标头信息-标头已发送 - How to remove warning Cannot modify header information - headers already sent by 警告:无法修改标头信息-标头已发送 - Warning: Cannot modify header information - headers already sent by 警告:无法修改标题信息 - 标题已在 wordpress 中发送 - Warning: Cannot modify header information - headers already sent by in wordpress 无法修改输出已发送的标题信息标题 - Cannot modify header information headers already sent by output 无法修改标题信息-标题已发送 - Cannot modify Header Information - headers already sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM