【问题标题】:How to fix Header new line error如何修复标题换行错误
【发布时间】:2013-12-19 17:46:10
【问题描述】:

我收到以下错误消息“标头可能不包含多个标头,检测到新行”我知道它表示已检测到新行,但我不知道该行来自何处​​。我试图修剪变量..我以不同的方式重新编写了标题行,但没有任何结果。我添加了 getallheaders 函数来查看正在传递的内容,但我在输出 $headers 中看不到新行或任何额外字符。即使使用 ob_start() 也无济于事。

<?php
ob_start();
include "catalog.obj";

session_start(); 
$catalogObj = $_SESSION['catalogObj'];

if (isset($_POST['st']))
$st = $_POST['st'];
else
$st = '0';
if (isset($_POST['num']))
$num = $_POST['num'];
else
$num = '0';
if (isset($_POST['type']))
$type = $_POST['type'];
else
$type = '0';
if (isset($_POST['rec']))
$rec = $_POST['rec'];
else
$rec = '0';
if (isset($_POST['option']))
$option = $_POST['option'];
else
$option = '0';

if(strcmp($_POST['submit'],"Reset Form") == 0)
{
    header("location: search_catalog.php?type=$type&firstTime=1"); 
    exit; 
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
    Header("Location: administration.php");
    exit;
}
else
{
    $inventory_id_num = $_POST['inventory_id_num'];
    $inventory_desc = $_POST['inventory_desc'];
    $inventory_revision = $_POST['inventory_revision'];
    $quantity = $_POST['quantity'];
    $catalog_status_id = $_POST['catalog_status_id'];
    $order_form_type_id = $_POST['order_form_type_id'];

    $catalogObj->inventory_id_num = $inventory_id_num;
    $catalogObj->inventory_desc = $inventory_desc;
    $catalogObj->inventory_revision = $inventory_revision;
    $catalogObj->quantity = $quantity;
    $catalogObj->catalog_status_id = $catalog_status_id;
    //$catalogObj->order_form_type_id = array();
    $catalogObj->order_form_type_id = $order_form_type_id;

    $count=count($order_form_type_id);
    for ($i=0; $i<$count; $i++) 
    {
        //print "order_form_type_id: $order_form_type_id[$i]<br>";
        if(strlen($order_form_type_id[$i]) > 0)
        {
            $catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
        }
    }

    if(strcmp($_POST['submit'],"Back to Order Form") == 0)
    {
        Header("Location: order_form.php?num=$num");
        exit;
    } 
    else
    {
        //$url = "type=".$type."option=".$option."rec=".$rec."st=".$st."num=".$num;
        Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num"); 
        //Header("location: search_catalog_handler.php?" . rawurlencode($url));
        if (function_exists('getallheaders'))
        {
            $headers = getallheaders();
            print_r( $headers);
        }       
        exit;
    }

}
    function getallheaders()
    {
           $headers = '';
       foreach ($_SERVER as $name => $value)
       {
           if (substr($name, 0, 5) == 'HTTP_')
           {
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
           }
       }
       return $headers;
    }

?>

【问题讨论】:

  • 什么是 catalog.obj ??
  • 从这里的语法高亮来看,这条线可能有问题Header("location: search_catalog_handler.php?type=$type&amp;option=$option&amp;rec=$rec&amp;st=$st&amp;num=$num");,而你正在使用POST。这样的 URL 理论上应该取自 GET
  • 这是导致问题的行!我正在更新一个 11 年前编写的 php 网站。原作者有一半时间使用帖子来传递标头函数中使用的数据。就像这里的情况一样。我有三个星期的时间来更新 108 个与此类似的 php 代码文件……如何在不重写所有内容的情况下解决这个问题?
  • 然后尝试使用rawurlencode,如$type = rawurlencode($type); Header("location: search_catalog_handler.php?type=$type&amp;option=$option&amp;rec=$rec&amp;st=$st&amp;num=$num");,从SO from this question的答案中找到
  • 我在另一个网站上找到了this Q&A。请参阅条目 #4,但您需要将 ereg_replace 替换为 preg_replacepreg_match 等效项。

标签: php header


【解决方案1】:

首先,感谢您的指点!上面代码中的问题在于 $st 变量。我对标题和重写它们不是很有经验,但我添加了以下条件声明:

if (!empty($_POST['st']))
{
    $st = $_POST['st'];
    $num = $_POST['num'];
    $type = $_POST['type'];
    $rec = $_POST['rec'];
    $option = $_POST['option'];
}

到我的代码开头,所以完整的代码是:

<?php
ob_start();
/*************************************
altered complete 12/20/2013
rjm
*************************************/
include "catalog.obj";

session_start(); 
$catalogObj = $_SESSION['catalogObj'];
if (!empty($_POST['st']))
{
    $st = $_POST['st'];
    $num = $_POST['num'];
    $type = $_POST['type'];
    $rec = $_POST['rec'];
    $option = $_POST['option'];
}

if(strcmp($_POST['submit'],"Reset Form") == 0)
{
    header("location: search_catalog.php?type=$type&firstTime=1"); 
    exit; 
}
elseif(strcmp($_POST['submit'],"Catalog Administration") == 0)
{
    Header("Location: administration.php");
    exit;
}
else
{
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    //exit;
    $inventory_id_num = $_POST['inventory_id_num'];
    $inventory_desc = $_POST['inventory_desc'];
    $inventory_revision = $_POST['inventory_revision'];
    $quantity = $_POST['quantity'];
    $catalog_status_id = $_POST['catalog_status_id'];
    $order_form_type_id = $_POST['order_form_type_id'];

    $catalogObj->inventory_id_num = $inventory_id_num;
    $catalogObj->inventory_desc = $inventory_desc;
    $catalogObj->inventory_revision = $inventory_revision;
    $catalogObj->quantity = $quantity;
    $catalogObj->catalog_status_id = $catalog_status_id;
    $catalogObj->order_form_type_id = $order_form_type_id;

    $count=count($order_form_type_id);
    for ($i=0; $i<$count; $i++) 
    {
        if(strlen($order_form_type_id[$i]) > 0)
        {
            $catalogObj->order_form_type_id[$i] = $order_form_type_id[$i];
        }
    }

    if(strcmp($_POST['submit'],"Back to Order Form") == 0)
    {
        Header("Location: order_form.php?num=$num");
        exit;
    } 
    else
    {
        Header("location: search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num"); 
        exit;
    }

}
?>

这允许从发送页面进行特定类型搜索(带参数)和一般类型搜索(无参数)。

【讨论】:

  • 啊,毕竟是Happy Ending。很高兴知道您找到了解决方案。
【解决方案2】:

假设catalog.obj 没有向浏览器输出任何信息(这也会导致错误),您的$type 变量看起来像是罪魁祸首,因为它是唯一的通配符。

请注意,您需要对脚本中要在 URI 中使用的所有 POSTed 变量执行以下操作:

尽管$type 可能是任何东西(它有时使用POSTed 变量),你应该在将它吐回你的标题之前清理它:

$type = urlencode($type); // Prepares the variable to be inserted in the URI
header("Location: search_catalog.php?type=$type&firstTime=1");

【讨论】:

  • 抱歉,这行代码不是问题所在。这是“标题(”位置:search_catalog_handler.php?type=$type&option=$option&rec=$rec&st=$st&num=$num");‌​”。有什么建议么? urlencode 没有帮助。
  • 如果你var_dump() 整个字符串以search_catalog... 开头,你会得到什么?
  • 抱歉,我现在无法打印出标题。它现在正在通过,但没有通过参数......如果你不介意我会在一段时间内和你一起回来,或者明天第一件事。漫长的一天......这让我在一天中的大部分时间都在兜圈子。
  • 这是我现在得到的"Array ([Connection] => keep-alive [Content-Length] => 571 [Content-Type] => application/x-www-form-urlencoded [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 [Accept-Encoding] => gzip, deflate [Accept-Language] => en-US,en;q=0.5 [Cookie] => PHPSESSID=ru8p2punaaaqp9kkvtio622jh5 [Host] => www.gmacestest.com [Referer] => gmacestest.com/search_catalog.php?firstTime=1 [User-Agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0)"
猜你喜欢
  • 2019-01-31
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 2020-12-19
相关资源
最近更新 更多