修改标头错误

时间:2013-07-26 03:38:56

标签: php

<?php 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;
    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
        // RUN IF THE CART IS EMPTY OR NOT SET
        $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
    } else {
        // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
        foreach ($_SESSION["cart_array"] as $each_item) { 
              $i++;
              while (list($key, $value) = each($each_item)) {
                  if ($key == "item_id" && $value == $pid) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
                      $wasFound = true;
                  } // close if condition
              } // close while loop
           } // close foreach loop
           if ($wasFound == false) {
               array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
           }
    }
    header("location: http://www.hirelogo.com/cart.php"); 
    exit();
}
?>

我遇到了解我正在犯的错误的问题。我最近刚刚使用较新版本的PHP切换到VPS,如果有帮助的话。这是错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/hirelogo/public_html/cart.php:5) in /home/hirelogo/public_html/cart.php on line 40 

第40行

  

标题(“location:http://www.hirelogo.com/cart.php”);

非常感谢任何理解这一点的帮助。另一个说明。在切换之前没有发生此问题。

3 个答案:

答案 0 :(得分:1)

如果在第40行调用header(),那么在您发布的代码的第一行之前,文档中有一些内容。因为我的标题位于代码的第29行。

在调用header()发布<?php开始标记之前的任何内容之前,您根本无法输出任何内容

答案 1 :(得分:1)

错误意味着在写入标题之前,“something”被推送到客户端。

  1. 检查是否有任何BOM(字节顺序标记)导致问题,如果您使用UTF-8或UTF-16,则可能就是这种情况。
  2. 检查请求文件开头的所有代码,直至脚本遇到错误,直至最后一个包含,并确保在您之前没有输出(如echosprintf)推出标题。

答案 2 :(得分:1)

if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {

尝试更改以下代码:

if (!isset($_SESSION["cart_array"]) || (isset($_SESSION['cart_array'])  && count($_SESSION["cart_array"]) < 1)) {

请注意您可能出现的错误?