Home > Article > Backend Development > Header("Location:xxx.php") jump problem in php_PHP tutorial
Headers have many functions in php. Here we will introduce to you some precautions when using headers to jump files. I hope this article can bring happiness to all my friends.
Some issues you should pay attention to with header("Location:login.php")
header("Location:") serves as a redirect statement for PHP. In fact, there are several points that need to be paid attention to during use.
1. There is no output before the header is required
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, which means to store the relevant output on the server side first. , wait for the appropriate opportunity before outputting, instead of running one sentence like now, outputting one sentence, and only reporting an error if the header statement is found.
The specific statements are: ob_start(); ob_end_clean(); ob_flush();......
2. Exit in time after header("Location:")
Otherwise, it will continue to execute. 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 being 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).
Therefore, the standard usage is:
The code is as follows | Copy code | ||||
|
If you want to have output before the header, you can modify the php.ini 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.
1. Introduction to related functions:
1. Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Note: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3. ob_get_contents: Returns the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; like ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or off absolute refresh
Usage: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x. This string can open/close the buffer, and the ob_implicit_flush function is the same as that. The default is to close the buffer. After turning on absolute output, each script output Send directly to the browser, no need to call flush()
ob_start() starts output buffering. At this time, PHP stops output, and all subsequent output is transferred to an internal buffer.
ob_get_contents() This function returns the contents of the internal buffer. This is equivalent to turning these outputs into strings.
ob_get_length() returns the length of the internal buffer.
ob_end_flush() ends the output buffer and outputs the contents of the buffer. After this, the output is normal output.
ob_end_clean() ends the output buffer and throws away the contents of the buffer.
For example, the var_dump() function outputs the structure and contents of a variable, which is useful during debugging.
But if the content of the variable contains HTML special characters such as < and >, it will not be visible when output to the web page. What should I do?
This problem can be easily solved using the output buffer function.
The code is as follows
|
Copy code
|
||||
var_dump($var); $out = ob_get_contents(); |
ob_end_clean();
The code is as follows | Copy code |
echo '' . htmlspecialchars( $out) . '' ; Or wait until the future, or send this string to the template (Template) and then output it. |