PHP Ob_start () function to implement the current page content cache output _php Tutorial

Source: Internet
Author: User
Tags file info
Page cache is to save the page to a file, the next time you read the file directly without querying the database, here we introduce the use of Ob_start () to achieve.


Cases

The code is as follows Copy Code

Ob_start (); Open buffer
Phpinfo (); Using the Phpinfo function
$info =ob_get_contents (); Get the contents of the buffer area and assign it to $info
$file =fopen (' Info.txt ', ' W '); Open File Info.txt
Fwrite ($file, $info); Write information to Info.txt
Fclose ($file); Close File Info.txt
or directly with file_put_content (' Info.txt ', $info);
?>

The above method can save the phpinfo information of different users.

Here we can focus on the use of this method of techniques, this method can be used to generate static pages convenient!

And using this method is more reasonable and efficient than using the File_get_conents () method.

Simply say an application, such as want to write the contents of Phpinfo () to a file, you can do this:

The code is as follows Copy Code

Ob_start ();
$phpinfo = Phpinfo ();
Write file
Ob_end_flush ();

Or, for this purpose:

Ob_start (); Open buffer
echo "Hellon"; Output
Header ("location:index.php"); redirect the browser to index.php
Ob_end_flush ();//output all content to the browser

The header () sends a file header to the browser, but if there is any output (including empty output, such as spaces, carriage returns, and line feeds) before the header (), the error will be made. But if the output is between Ob_start () and Ob_end_flush (), there is no problem. Because the buffer is opened before the output, the characters behind the echo are not output to the browser, but remain on the server, knowing that flush will be used to output, so the header () will execute normally.

Of course, Ob_start () can also have parameters, and the parameter is a callback function. Examples are as follows:

copy code

< PHP
Function callback ($buffer)
{
//Replace all the apples with oranges
Return (str_replace ("Apples", "oranges", $buffer));
}
Ob_start ("callback");
?
< html;
< body;

It's like comparing apples to oranges.


</body;
< php
Ob_end_flush ();
.

The above program will output:

< html
< body;
< P>it ' s like comparing oranges to oranges. !--p-->
</body;

As for more, go to the official website of the manual to see it.

http://www.bkjia.com/PHPjc/632725.html www.bkjia.com true http://www.bkjia.com/PHPjc/632725.html techarticle page cache is to save the page to a file, the next time you read the file directly without querying the database, here we introduce the use of Ob_start () to achieve. Example code is the following copy generation ...

  • Related Article

    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.