Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the cause of headersalreadysent errors?

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report