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 ...