Php header new line

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Header may not contain more than a single header, new line detected #6782

Bug: Header may not contain more than a single header, new line detected #6782

Comments

PHP Version

CodeIgniter4 Version

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter )

Which operating systems have you tested for this bug?

Which server did you use?

Database

What happened?

I’m getting this error in the log

CRITICAL - 2022-10-27 17:15:29 --> Uncaught ErrorException: Header may not contain more than a single header, new line detected in /home/dsf/web/vendor/codeigniter4/framework/system/HTTP/ResponseTrait.php:475 Stack trace: #0 /home/dsf/web/core_modules/Debug/Exceptions.php(51): CodeIgniter\Debug\Exceptions->errorHandler() #1 [internal function]: Debug\Exceptions->errorHandler() #2 /home/dsf/web/vendor/codeigniter4/framework/system/HTTP/ResponseTrait.php(475): header() #3 /home/dsf/web/vendor/codeigniter4/framework/system/HTTP/ResponseTrait.php(445): CodeIgniter\HTTP\Response->sendHeaders() #4 /home/dsf/web/vendor/codeigniter4/framework/system/Debug/Exceptions.php(132): CodeIgniter\HTTP\Response->send() #5 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler() #6 thrown in SYSTEMPATH/HTTP/ResponseTrait.php on line 475. 1 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler() 

Steps to Reproduce

I am unable to reproduce the issue manually. I happens rarely. The site is serving hundred thousands requests daly and I’m not seeing the issue every days. It happens only on some days once or twice.

Читайте также:  Python apply function to numpy array

What would be the best way to get more details on the issue. I can only add some additional logging so that we can follow the where the new line was outputted before the issue happens.

Looking at the stack trace I don’t see any of my custom code being loaded. So it has to be somewhere in the CI classes.

Expected Output

The critical error should not happen

Anything else?

Using
Nginx + Php 8.1 with PHP-FPM on AlmaLinux 9 but i was noticing the same issue with CentOS 7 and php7.X/8.0 setup. So it should not be related to PHP version

The text was updated successfully, but these errors were encountered:

Источник

Tournas Dimitrios

Why New Line (“\n”) in PHP is Not Working September 15, 2012

One thing that took me a long time until I could give an explanation was the new line tag (\n) in PHP-scripts . I copied PHP-script examples from books and other documentation , the “\n” line feed tag was ignored by my browser . Lines of text that supposed to be displayed on separate lines , were just concatenated into the same line . If text (with “\n” tags) were stored into a text-file and that file was opened directly into the browser then each text-line was displayed as it should (on separate lines) . To circumvent this “unknown-bug” I had to replace all “\n” strings by an “< b r/> ” tag . But then , when I opened these files in my browser , “br” tags where not translated as new-lines (just “br” was displayed on the screen) . I couldn’t give a theoretical explanation for this “browser-behavior” and arbitrary designated it as a “unknown-bug” 🙂 . As time moved on , I learned “step-by-step” new PHP concepts , until one day , I discovered the hidden development tool that each browser has embedded . This tool helps us to shed light into actions that occur behind the scenes during web-communications . Actually the browser displays the result of different actions (no matter which , Ajax – JQuery – CSS) . And besides all these actions , browser behaviour is customisable by header-fields . Wikipedia has a long list of these header fields , but the most important field (in focus of this article) , is the Content-Type field .

Читайте также:  Css высота элемента узнать

PHP outputs to a browser (usually) , which renders the output as HTML by default . In HTML , a newline tag (\n) is treated as white-space . Using header(‘Content-type: text/plain’); will cause the output to be rendered as plain text (the “\n” will render text on separate lines ) . Remember that header() must be called before any actual output is sent , either by normal HTML tags, blank lines in a file, or from PHP . A simple code could be as follows :

Hope this article has helped some [new]developers not to designate the “\n” tag as “unknown-bug” (as I did in the past 🙂 ) .

Источник

php — 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.

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

Answer

Solution:

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'])) 

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

 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 "
"; print_r($_POST); echo "

"; //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 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.

Answer

Solution:

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 POST ed 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 POST ed 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"); 

Источник

Оцените статью