Redirection of header ("Location: xxx. php") in php _ PHP Tutorial

Source: Internet
Author: User
Tags exit in
Redirection of header ("Location: xxx. php") in php. In php, the header has many functions. here we will introduce some precautions when using the header for file redirection. I hope this article will bring happiness to all of you. Header (Location has many functions in php. here we will introduce some precautions when using headers for file redirection. I hope this article will bring happiness to all of you.

Header ("Location: login. php ")
Header ("Location:") is the php redirection statement. In fact, there are several points worth attention in use.

1. no output is required before the header.

However, many times we have output a lot of items before the header. if the header is used again, it is obviously incorrect. here we enable the concept of ob, ob means to store the relevant output on the server side and wait for the proper time to output the output, instead of running a sentence like the current one, and output a sentence. it is found that the header statement can only report an error.

Specific statements include: ob_start (); ob_end_clean (); ob_flush ();.........


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

Otherwise, he will continue to execute the command. although you cannot see the corresponding data on the browser side, if you perform packet capture analysis, you will see that the following statement is also being executed. It is also delivered 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:

Ob_start ();

........

If (something ){

Ob_end_clean ();

Header ("Location: yourlocation ");

Exit;

Else {

..........

Ob_flush (); // can be omitted


To output data before the header, 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 header information sent using header () or setcookie (). it only applies to data blocks similar to echo () and PHP code.

1. related functions:

1. Flush: refresh the buffer content and output it.
Function format: flush ()
Note: This function is frequently used and highly efficient.
2. ob_start: Open the output buffer.
Function format: void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
3. ob_get_contents: returns the content of the internal buffer.
Usage: string ob_get_contents (void)
Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.
4. ob_get_length: return the length of the internal buffer.
Usage: int ob_get_length (void)
Note: This function returns the length of the current buffer. it is the same as ob_get_contents if the output buffer is not activated. Returns FALSE.
5. ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush (void)
Note: This function sends the content of the output buffer (if any ).
6. ob_end_clean: delete the content of the internal buffer and disable the internal buffer.
Usage: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it!
7. ob_implicit_flush: enable or disable absolute refresh
Usage: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $ | = x. This string can enable/disable the buffer, while the ob_implicit_flush function is the same as that. the buffer is disabled by default, after the absolute output is enabled, each script output is directly sent to the browser, and you do not need to call flush ()

Ob_start () starts to output the buffer, and PHP stops the output. after that, the output is forwarded to an internal buffer.
The ob_get_contents () function returns the content of the internal buffer. this is equivalent to converting these outputs into strings.
Ob_get _ length () returns the length of the internal buffer.
Ob_end_flush () ends the output buffer and outputs the content in the buffer. after that, the output is normal.
Ob_end_clean () ends the output buffer and discards the content in the buffer.
For example, the var_dump () function outputs the structure and content of a variable, which is useful during debugging.
However, if the variable content contains special HTML characters such as <,>, the output will be invisible to the webpage. what should I do?

The output buffer function can easily solve this problem.

The code is as follows:

Ob_start ();
Var_dump ($ var );
$ Out = ob_get_contents ();
Ob_end_clean ();

At this time, the output of var_dump () already exists in $ out. you can output it now:

The code is as follows:
Echo'
' . htmlspecialchars($out) . '
';

Or wait for the future, or send this string to the Template and then output it.

Bytes. Header (Location...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.