Several problems should be noticed in header ("Location:login.php")
Header ("Location:") as a steering statement for PHP. In fact, in use, he has a few points to pay attention to the place.
1. No output required before header
But many times in front of the header we have output a lot of things, at this time if the header again, it is obviously wrong, here we have enabled the concept of an OB, OB means to store the output on the server side, waiting for the appropriate time to output, instead of running a sentence like now, Output a sentence, found the header statement can only error.
The specific statements are: Ob_start (); Ob_end_clean (); Ob_flush (); .....
2, in the header ("Location:") to promptly exit
Otherwise he will continue to execute, although you do not see the corresponding data on the browser side, but if you do the packet analysis, you will see the following statement is also in progress. and is sent to the browser client, but is not executed by the browser as HTML (the browser performed a header to turn the operation).
Therefore, the standard method of use is:
Ob_start ();
........
if (something) {
Ob_end_clean ();
Header ("Location:yourlocation");
Exit
else{
..........
Ob_flush (); can be omitted
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 gives you the freedom to control the outputs of the data in your script. It is very useful, especially when you want to output a file header after the data has been output. The output control function does not affect the header information sent using header () or Setcookie (), only data blocks that resemble Echo () and PHP code are useful.
Introduction of related functions:
1, Flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: 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
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()
Ob_start () starts the output buffer, when PHP stops the output, after which the output is transferred to an internal buffer.
Ob_get_contents () This function returns the contents of the internal buffer. This is tantamount to turning these outputs into strings.
Ob_get_ Length () returns the size of the internal buffer.
Ob_end_flush () ends the output buffer and outputs the contents of the buffer. After that, the output is normal output.
Ob_end_clean () ends the output buffer and discards the contents of the buffer.
For example, the Var_dump () function outputs the structure and contents of a variable, which is useful when debugging.
But if the contents of the variable have <, > and other special characters of HTML, the output to the Web page is not visible. What do we do?
It is easy to solve this problem with the output buffering function.
Ob_start ();
Var_dump ($var);
$out = Ob_get_contents ();
Ob_end_clean ();
At this time the output of var_dump () already exists $out. You can output it now:
Echo ' <pre> '. Htmlspecialchars ($out). ' </pre> ';
Or wait until the future, or send the string to the template and then output.
Php_header Location Usage Precautions