Home  >  Article  >  Backend Development  >  header("Location:login.php")_PHP tutorial

header("Location:login.php")_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:08843browse

Several issues that should be paid attention to in header("Location:login.php") header("Location:") is used as a redirect statement in 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 enable 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.

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 method is:

ob_start();

 ...

 if ( something ){

ob_end_clean();

header("Location: yourlocation");

exit;

else{

 …………

ob_flush(); //can be omitted

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; the same as 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)

Explanation: 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 is sent directly to the browser, no need to call flush()

ob_start() starts output buffering. At this time, PHP stops outputting. After that, all 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 discards the contents of the buffer.

For example, the var_dump() function outputs the structure and content of a variable, which is very useful during debugging.

But if the content of the variable contains HTML special characters such as < , >, 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.

ob_start();

var_dump($var);

 $out = ob_get_contents();

ob_end_clean();

At this time, the output of var_dump() is already stored in $out. You can output it now:

echo '' . htmlspecialchars($out) . '' ;

Or wait until the future, or send this string to the template (Template) and then output it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371435.htmlTechArticleHeader(Location:login.php) Several issues that should be paid attention to header(Location:) as a redirect statement in php . In fact, there are several points that need to be paid attention to during use. 1. There is no requirement before the header...
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