PHP - 无法修改标头信息 - 标头已发送(输出开始于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38028043/
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
PHP - cannot modify header information - headers already sent by (output started at
提问by Prince
I am working with PHP and I got this message
我正在使用 PHP,我收到了这条消息
cannot modify header information - headers already sent by (output started at
无法修改标头信息 - 标头已由(输出开始于
after executing a code which looks like this:
执行如下代码后:
<?php
if (condition) {
if (condition) {
//Statement
}
$to = $_POST['email'];
$subject = "Registration Confirmation";
$message = '
<html>
<head>
<title>'.$subject.'</title>
</head>
</html>
';
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
mail($to,$subject,$message,$headers);
header('Location: reset.php?username='.$username);
exit();
}else {
//statement
}
Based on this answer from Stackoverflow, I think the error is coming from the HTML code in the variable $message
. I don't really know how I can modify the content of that variable to avoid the error.
根据Stackoverflow 的这个答案,我认为错误来自变量中的 HTML 代码$message
。我真的不知道如何修改该变量的内容以避免错误。
Kindly help me solve this problem.
请帮我解决这个问题。
回答by Anay Pareek
ob_start();
ob_start();
html is also sending output that is why this is happening so use ob_start(); in top of your code, hope this solves your problem.
html 也发送输出,这就是为什么会发生这种情况,所以使用 ob_start(); 在您的代码之上,希望这能解决您的问题。
回答by Sylter
From php documentation:
从php 文档:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。
So move header('Location: reset.php?username='.$username);
before any other kind of output.
所以header('Location: reset.php?username='.$username);
在任何其他类型的输出之前移动。
By the way, this question was already answered:
顺便说一下,这个问题已经回答了:
回答by jayasena palihakoon
just change your code to echo'window.location="dashboard.php?id=40";'; then it will work
只需将您的代码更改为 echo'window.location="dashboard.php?id=40";'; 然后它会工作
回答by jayasena palihakoon
change your code:-
echo'window.location="username='.$username";';
exit();
更改您的代码:-
echo'window.location="username='.$username";';
出口();