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,
- No space is allowed between location and:; otherwise, an error occurs.
- No output is allowed before headers are used.
- The PHP code after the header is executed.
The test result is correct.
, The test is probably referringheader('Location: http://segmentfault.com/');
MediumLocation
And:
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,
- No space is allowed between location and:; otherwise, an error occurs.
- No output is allowed before headers are used.
- The PHP code after the header is executed.
The test result is correct.
, The test is probably referringheader('Location: http://segmentfault.com/');
MediumLocation
And:
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.echo
Orprint
But 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.