Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the cause of headersalreadysent errors?

Shulou Source: shulou.com Published: 2022-06-03 11:01:56 11月15日 Update

This article will give you a detailed explanation of the causes of headersalreadysent errors. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

The method that headersalreadysent sends or modifies HTTP headers must be called before any output is output. Otherwise, there will be an error Warning:Cannotmodifyheaderinformation-headersalreadysent (outputstartedatscript:line). These methods can modify (modify) the HTTP header information.

Why headersalreadysent errors occur

To understand why the HTTPheader must be sent before the output, it is necessary to take a look at a typical HTTP correspondence. The PHP script is mainly used to generate HTML, but it also sends a series of HTTP/CGI header messages to the web server:

HTTP/1.1200OK

Powered-By:PHP/5.3.7

Vary:Accept-Encoding

Content-Type:text/html;charset=utf-8

PHPpageoutputpage

Content

Somemoreoutputfollows...

And

The page or output always follows the header message. PHP must first send the header information to the web server, and it can only send it once, after which it can no longer be modified.

When PHP first receives the output (print,echo,) it clears all collected header information. After that, it can output all the content it wants to output, but it is impossible to send HTTP headers.

How do we find out where it is, headersalreadysent?

The header () header information contains all the information relevant to the problem:

Warning:Cannotmodifyheaderinformation-headersalreadysentby (outputstartedat/www/usr2345/htdocs/auth.php:52) in/www/usr2345/htdocs/index.phponline100

In the above warning, line100 points to the number of script lines that failed to call header ().

The message outputstarted in parentheses is even more important. It indicates the source of the output that precedes header (). In this case, line 52 of auth.php, which is where you're looking for premature output.

Typical reasons are these:

Print,echo

Intentional output of print and echo statements will interrupt the opportunity to output HTTP header information. The application flow must be reorganized to avoid this behavior, and can be reorganized using function and templates to ensure that the header () call is made before the information is written out.

This is the end of the article on "what are the causes of headersalreadysent errors?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Information output reason article typical content method more server script service reorganization good practical important necessary example parenthesis place is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL Huawei Shulou Tech Info Apple