Home  >  Article  >  Backend Development  >  Detailed analysis of PHP header jump

Detailed analysis of PHP header jump

高洛峰
高洛峰Original
2016-12-20 11:06:091501browse

I don’t know much about this function. It is usually used to set character encoding or page jump. I won’t go into details about setting encoding.

In the past, when it came to header jumps, I just knew that any output was strictly prohibited before jumping, otherwise something would go wrong, but today I saw a post that made me get to know headers again:

1. No output is required before headers

But many times We have already output a lot of things before the header. If we use the header again at this time, it will obviously be wrong. Here we have enabled the concept of ob. Ob means to store the relevant output on the server side first and wait for the appropriate opportunity. Output, instead of running one sentence like now, output one sentence, and if you find the header statement, you can only report an error.

The specific statements are: ob_start(); ob_end_clean(); ob_flush();.........

2. Exit in time after header("Location:")

otherwise It will continue to be executed. Although you cannot see the corresponding data appearing on the browser side, if you perform packet capture analysis, you will see that the following statements are also executed. And it is sent to the browser client, but it is not executed as html by the browser (the browser executes the header and performs the redirection operation).

So, the standard usage method is:

ob_start();

.....

if ( something ){

ob_end_clean();

header("Location: yourlocation" ; File

output_handler =mb_output_handler

or output_handler =on

The Output Control function allows you to freely control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output. The output control function does not affect the file header information sent using header() or setcookie(), only the data blocks similar to echo() and PHP code.

For more articles related to detailed analysis of PHP header jump, please pay attention to PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn