“警告:无法修改标头信息 - 已经发送的标头”错误

时间:2009-12-16 03:15:19

标签: php mysql

  

可能重复:
  Headers already sent by PHP

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

  

警告:无法修改标头   信息 - 已经发送的标题   (输出始于   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>     

提前感谢你!

6 个答案:

答案 0 :(得分:39)

第45-47行:

?>

<?php

那是发送几个新行作为输出,因此已经调度了标题。只需删除这三行(毕竟它是一个大的PHP块,不需要结束PHP解析然后重新启动它),以及60-62行的类似块,它会工作。

请注意,您获得的错误消息实际上为您提供了大量信息,以帮助您自己找到它:

  

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

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

答案 1 :(得分:38)

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

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

答案 2 :(得分:24)

检查 文档编码

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

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

答案 3 :(得分:9)

您的?><?php代码之间的空白行正在发送给客户。

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

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

删除那些不必要的标签,将其全部放在一个大<?php块中。

答案 4 :(得分:8)

你的php标签之外可能有空格。

答案 5 :(得分:6)