Page caching is to save the page to a file and directly call the file the next time it is read without querying the database. Here we introduce the use of ob_start() to achieve it.
Example
The code is as follows
代码如下 |
复制代码 |
ob_start(); //打开缓冲区
phpinfo(); //使用phpinfo函数
$info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info
$file=fopen(’info.txt’,’w’); //打开文件info.txt
fwrite($file,$info); //写入信息到info.txt
fclose($file); //关闭文件info.txt
//或直接用 file_put_content('info.txt',$info);
?>
|
|
Copy code
|
ob_start(); //Open buffer
phpinfo(); //Use phpinfo function
$info=ob_get_contents(); //Get the contents of the buffer and assign it to $info
$file=fopen(’info.txt’,’w’); //Open the file info.txt
fwrite($file,$info); //Write information to info.txt
代码如下 |
复制代码 |
ob_start();
$phpinfo = phpinfo();
//写入文件
ob_end_flush();
或者还有这样的用途:
ob_start(); //打开缓冲区
echo "Hellon"; //输出
header("location:index.php"); //把浏览器重定向到index.php
ob_end_flush();//输出全部内容到浏览器
|
fclose($file); //Close the file info.txt
//Or use file_put_content('info.txt',$info) directly;
?>
代码如下 |
复制代码 |
< ? 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 >
< / html >
< ?php
ob_end_flush();
? >
以上程序会输出:
< html >
< body >
< p>It's like comparing oranges to oranges. p>
< / body >
< / html >
|
The above method can save the phpinfo information of different users. |
Here we can focus on the usage skills of this method. Using this method can achieve the convenience of generating static pages!
And using this method is more reasonable and efficient than using file_get_conents().
Let’s talk about an application simply. For example, if you 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 to file
ob_end_flush();
Or there is such a use:
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
Header() will send a file header to the browser, but if there is any output before header() (including empty output, such as spaces, carriage returns and line feeds), an error will be reported. But if the output is between ob_start() and ob_end_flush(), there will be no problem. Because the buffer is opened before output, the characters after echo will not be output to the browser, but will be retained on the server. They will not be output until flush is used, so header() will execute normally.
Of course, ob_start() can also have parameters, and the parameter is a callback function. Examples are as follows:
The code is 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 >
< / html >
< ?php<🎜>
ob_end_flush();<🎜>
?>
The above program will output:
< html >
< body >
< p>It's like comparing oranges to oranges. p>
< / body >
< / html >
For more information, just go to the manual on the official website.
http://www.bkjia.com/PHPjc/632725.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632725.htmlTechArticlePage caching is to save the page to a file, and call the file directly the next time it is read without querying the database. Here we introduce the use of ob_start() to achieve it. Example code is as follows Copy code...
|
|