【问题标题】:"Warning: Cannot modify header information - headers already sent by" error [duplicate]“警告:无法修改标头信息 - 标头已由”错误 [重复]
【发布时间】:2010-12-27 02:09:16
【问题描述】:

我每次尝试提交删除表单时都会收到此错误。

警告:无法修改标题 信息 - 已发送的标头 (输出开始于 C:\xampp\htdocs\speedycms\deleteclient.php:47) 在 C:\xampp\htdocs\speedycms\deleteclient.php 在第 106 行

我的代码有问题吗?我需要进行哪些更改才能使其正常工作?

<?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>     

【问题讨论】:

  • 谷歌搜索会是理想的,是的......但我想了解为什么以及我犯了错误。
  • 我在上传文件到服务器时遇到了这样的问题,服务器甚至支持 PHP5.3 使用 PHP 5.6 或更高版本的服务器。
  • 主要是php的输出不友好造成的。例如 之间的空格 white space white space ;回显或打印在 header("Location: .../.../....php") 之前出现的数据。它破坏了代码转换

标签: php mysql


【解决方案1】:

您的 php 标签外可能有空格。

【讨论】:

    【解决方案2】:

    ?&gt;&lt;?php 标记之间的空白行正在发送到客户端。

    当第一个被发送时,它会导致你的标题首先被发送。

    一旦发生这种情况,您就不能再修改标头了。

    删除那些不必要的标签,将它们全部放在一个大的&lt;?php 块中。

    【讨论】:

      【解决方案3】:

      第 45-47 行:

      ?>
      
      <?php
      

      这会发送几个换行符作为输出,因此标头已被分派。去掉这3行(毕竟都是一个大的PHP块,不用结束PHP解析再重新开始),以及60-62行的类似块,就可以了。

      请注意,您收到的错误消息实际上为您提供了很多信息来帮助您自己找到它:

      警告:无法修改标题 信息 - 已发送的标头 (输出开始于 C:\xampp\htdocs\speedycms\deleteclient.php:47) 在 C:\xampp\htdocs\speedycms\deleteclient.php 在第 106 行

      两个粗体部分告诉您在标题之前发送输出的项目在哪里(第 47 行)以及在输出之后尝试发送标题的项目在哪里(第 106 行)。

      【讨论】:

        【解决方案4】:

        这通常发生在您开始会话之前脚本出现意外输出时。使用您当前的代码,您可以尝试使用输出缓冲来解决它。

        尝试在脚本的最顶部添加对 ob_start(); 函数的调用,并在文档的最后添加对 ob_end_flush(); 的调用。

        【讨论】:

        • 这次我非常感谢你救了我。我已经浪费了很多。谢谢。
        • 也为我工作
        【解决方案5】:

        检查文档编码

        我遇到了同样的问题。我在 Windows XP 上使用 Notepad++ 和 WampServer 开发以在本地运行 Apache,一切都很好。上传到在 Unix 上使用 Apache 的托管服务提供商后,我收到了这个错误。我没有多余的 PHP 标记或结束标记后多余的行中的空格。

        对我来说,这是由文本文档的编码引起的。我在 Notepad++(在 Encoding 选项卡下)中使用了“转换为 UTF-8 without BOM”选项并重新加载到 Web 服务器。问题已修复,无需更改代码/编辑。

        【讨论】:

        • 这也解决了我的问题 :-D 你知道是否有办法在 PHP 中设置它,这样你就不必在 Notepad++ 中重新保存它了吗?我知道你可以为 utf8 做到这一点(使用 charset 或 $conn->exec("set names utf8")),但我无法找到没有 BOM 的方法
        • 它是我们从不擅长的炉排解决方案。它节省了我很多时间,谢谢。
        猜你喜欢
        • 2012-02-28
        • 2012-03-31
        • 1970-01-01
        • 2013-11-24
        • 2013-07-09
        • 2011-01-15
        • 2011-05-09
        相关资源
        最近更新 更多