royjm
royjm

Reputation: 107

How to fix Header new line error

I am receiving the following error message "Header may not contain more than a single header, new line detected" I know it says that a new line has been detected, but I cannot figure where this line is coming from. I have tried to trim the variables..I have re-written the header line in different ways, without any result. I added the getallheaders function to see what was being passed, but I see no new line or any extra characters in the output $headers. Even using ob_start() does not help.

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

?>

Upvotes: 1

Views: 1602

Answers (2)

royjm
royjm

Reputation: 107

First, thanks for the pointers! The problem in the above code was with the $st variable. I am not very experienced with headers and rewriting them but I had add the following conditinal statement:

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

To the beginning of my code, so it the complete code is:

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

}
?>

This allows for a specific type search (with parameters) and a general type search (no parameters) from the sending page.

Upvotes: 1

jterry
jterry

Reputation: 6269

Assuming that catalog.obj does not output any information to the browser (which would result in an error as well), your $type variable looks like the culprit since it's the only wildcard.

Note that you'll need to do the following for all POSTed variables in your script that you want to use in a URI:

Sine it's possible that $type could be anything (it's using the POSTed variable sometimes), you should clean it up before spitting it back out in your header:

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

Upvotes: 0

Related Questions