Phpheader function problems, such as the header cannot have actual output before

Source: Internet
Author: User
Link to the original article: PHPHeader has found this article by summarizing several issues that should be paid attention to during page Jump with php skills. At the beginning, I mentioned three precautions for PHPheader (), location and ":. There cannot be any output before using the header... link to the original article: PHP Header for page Jump should be noted in the summary of several issues Php skillsFoot home

I found this article and started with three notes on PHP header,

  1. No space is allowed between location and:; otherwise, an error occurs.
  2. No output is allowed before headers are used.
  3. The PHP code after the header is executed.

The test result is correct.

, The test is probably referringheader('Location: http://segmentfault.com/');MediumLocationAnd:There can be no space, but it can.

It is mainly at. There is no doubt about the output before the header. I tested the following code and can jump to it successfully:

asdf
  

How can I test it to report an error? In the php manual, "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 "what does it mean?

Reply content:

Link to the original article: PHP Header for page JumpPhp skillsFoot home

I found this article and started with three notes on PHP header,

  1. No space is allowed between location and:; otherwise, an error occurs.
  2. No output is allowed before headers are used.
  3. The PHP code after the header is executed.

The test result is correct.

, The test is probably referringheader('Location: http://segmentfault.com/');MediumLocationAnd:There can be no space, but it can.

It is mainly at. There is no doubt about the output before the header. I tested the following code and can jump to it successfully:

asdf
   

How can I test it to report an error? In the php manual, "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 "what does it mean?

In principle, the String in the header function will be directly added to the HTTP header. Therefore, if

Location: http://with.blank.com

This type of HTTP header does not comply with HTTP specifications, and it is likely that the entire HTTP Response cannot be parsed.
Fortunately, PHP seems to have fixed the problem of the HTTP Header Format for you.

The problem of starting an HTTP header does not need to be output after the HTTP content is output. However, the Server caches the output.echoOrprintBut at that time, the server has not output the content as an HTTP packet, and you still have the opportunity to modify the HTTP header.

If you just want to trigger an error, it's easy to output an HTTP content that exceeds the cache size before the header. Below are examples of poor performance.


    

@ Scaret should be correct, but it is actually a cache problem.


     ";ob_flush();flush();header('Location: http://segmentfault.com/');

Get

Warning: Cannot modify header information - headers already sent by (output started at ...

If you change the html tag to the PHP echo, an error is returned.
The html tag in the php script is sent to the user only after the PHP script is executed.
So in your demoheader('Location : http://segmentfault.com/');There is no output.

header('Location : http://segmentfault.com/');This method sends a 302 redirection in the response header. The Response Header must be output in advance than the response body.
You can use fiddler to capture packets for details.

Hello, I have the same question with you. I also output it before header 200. According to the method on the second floor, I even output:

// Do not add the following code to your browser. It may be broken down to illustrate for ($ I = 0; $ I <1e100; $ I ++) echo $ I;

There are still no errors in the client browser when there are more than dozens of megabytes of data. I guess it may be that the web server (such as apache or nginx) has optimized this situation, maybe there is no output before the header, which is just an old suggestion. It may be caused by a bug in the old php version. It is estimated that this bug has been fixed, so no error is reported.

In addition, some people say that the client output is not performed immediately, so I use the following code:

for($i = 0; $i < 1e5; $i++) echo $i;ob_flush();flush();header('HTTP/1.1 200 OK');

No error is reported. Who can answer it?

I can only say that the current php can recognize the header output, extract the header output separately, and output the output in response first. Then, the output problem is avoided.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.