php “警告:无法修改标头信息 - 标头已发送”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1912029/
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
"Warning: Cannot modify header information - headers already sent by" error
提问by methuselah
Possible Duplicate:
Headers already sent by PHP
可能重复:
PHP 已发送的标头
i keep receiving this error each time i try to submit the a form deletion form.
每次尝试提交表单删除表单时,我都会收到此错误。
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\speedycms\deleteclient.php:47) in C:\xampp\htdocs\speedycms\deleteclient.php on line 106
警告:无法修改标头信息 - 标头已由(输出开始于 C:\xampp\htdocs\speedycms\deleteclient.php:47)在 C:\xampp\htdocs\speedycms\deleteclient.php 第 106 行发送
is there something wrong with my code? what do i need to change to make it work?
我的代码有问题吗?我需要改变什么才能使它工作?
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && true) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
$MM_referrer .= "?" . $QUERY_STRING;
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<?php
require_once('Connections/speedycms.php');
$client_id = mysql_real_escape_string($_GET['id']);
$con = mysql_connect($hostname_speedycms, $username_speedycms, $password_speedycms);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("speedycms") or die(mysql_error());
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
if ((isset($_GET['id'])) && ($_GET['id'] != "") && (isset($_POST['deleteForm']))) {
$deleteSQL = sprintf("DELETE FROM tbl_accident WHERE id=%s",
GetSQLValueString($_GET['id'], "int"));
mysql_select_db($database_speedycms, $speedycms);
$Result1 = mysql_query($deleteSQL, $speedycms) or die(mysql_error());
$deleteGoTo = "progress.php";
if (isset($_SERVER['QUERY_STRING'])) {
$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
$deleteGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $deleteGoTo));
}
mysql_select_db($database_speedycms, $speedycms);
$query_delete = "SELECT * FROM tbl_accident WHERE id=$client_id";
$delete = mysql_query($query_delete, $speedycms) or die(mysql_error());
$row_delete = mysql_fetch_assoc($delete);
$totalRows_delete = mysql_num_rows($delete);
?>
<p class="form2">Are you sure you wish to <b>delete</b> the record for <?php echo $row_delete['clientName']; ?>?</p>
<form name="form" method="POST" action="<?php echo $deleteAction; ?>">
<p class="form2"><input type="submit" value="Yes" />
<input name="no" type="button" id="no" value="No" />
</p>
<input type="hidden" name="deleteForm" value="form" />
</form>
thanking you in advance!
提前谢谢你!
回答by Chris Gutierrez
This typically occurs when there is unintended output from the script before you start the session. With your current code, you could try to use output buffering to solve it.
这通常发生在启动会话之前脚本出现意外输出时。使用您当前的代码,您可以尝试使用输出缓冲来解决它。
try adding a call to the ob_start();function at the very top of your script and ob_end_flush();at the very end of the document.
尝试ob_start();在脚本的最顶部和ob_end_flush();文档的最后添加对函数的调用。
回答by Amber
Lines 45-47:
第 45-47 行:
?>
<?php
That's sending a couple of newlines as output, so the headers are already dispatched. Just remove those 3 lines (it's all one big PHP block after all, no need to end PHP parsing and then start it again), as well as the similar block on lines 60-62, and it'll work.
那是发送几个换行符作为输出,所以标题已经被分派了。只需删除这 3 行(毕竟它都是一个大的 PHP 块,无需结束 PHP 解析然后重新开始),以及第 60-62 行的类似块,它就可以工作了。
Notice that the error message you got actually gives you a lot of information to help you find this yourself:
请注意,您收到的错误消息实际上为您提供了很多信息,可帮助您自己查找:
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\speedycms\deleteclient.php:47) in C:\xampp\htdocs\speedycms\deleteclient.php on line 106
警告:无法修改标头信息 - 标头已由(输出开始于 C:\xampp\htdocs\speedycms\deleteclient.php:47)在 C:\xampp\htdocs\speedycms\deleteclient.php 中第 106 行发送
The two bolded sections tell you where the item is that sent output before the headers (line 47) and where the item is that was trying to send a header after output (line 106).
两个粗体部分告诉您在标题之前发送输出的项目在哪里(第 47 行),以及在输出之后尝试发送标题的项目在哪里(第 106 行)。
回答by staticboy
Check the document encoding.
检查文档编码。
I had this same problem. I develop on Windows XP using Notepad++ and WampServer to run Apache locally and all was fine. After uploading to hosting provider that uses Apache on Unix I got this error. I had no extra PHP tags or white-space from extra lines after the closing tag.
我有同样的问题。我使用 Notepad++ 和 WampServer 在 Windows XP 上开发以在本地运行 Apache,一切都很好。上传到在 Unix 上使用 Apache 的托管服务提供商后,我收到此错误。我没有多余的 PHP 标签或结束标签后的多余行中的空白。
For me this was caused by the encodingof the text documents. I used the "Convert to UTF-8 without BOM" option in Notepad++(under Encodingtab) and reloaded to the web server. Problem fixed, no code/editing changes required.
对我来说,这是由文本文档的编码引起的。我在 Notepad++(在编码选项卡下)中使用了“Convert to UTF-8 without BOM”选项并重新加载到 Web 服务器。问题已修复,无需代码/编辑更改。
回答by Anon.
Those blank lines between your ?>and <?phptags are being sent to the client.
你?>和<?php标签之间的那些空行被发送到客户端。
When the first one of those is sent, it causes your headers to be sent first.
当第一个发送时,它会导致首先发送您的标头。
Once that happens, you can't modify the headers any more.
一旦发生这种情况,您将无法再修改标题。
Remove those unnecessary tags, have it all in one big <?phpblock.
删除那些不必要的标签,将其全部放在一个大块中<?php。
回答by AJ.
There is likely whitespace outside of your php tags.
您的 php 标签之外可能有空格。

