Use OB series function to implement PHP Web page static, obphp
The PHP execution page is pre-converted to HTML and is one of the so-called static PHP methods.
There are other methods, such as template substitution method, Opcache, etc.
The effect of static:
Improve the response speed of the website, reduce the load of the server;
SEO for the search engine;
The use of PHP output buffer directly written HTML, timed background time to scan the site, is relatively easy to achieve the static page.
Before Static:
<?phpfor ($i =0; $i <1000; $i + +) {echo "Bull
";}? >
Static:
<?phpob_start (); for ($i =0; $i <1000; $i + +) {echo "Bull
";} $str = Ob_get_contents (); Ob_end_clean () $fp = fopen ("test.html", "W"), Fwrite ($fp, $STR); fclose ($FP); echo "Success Write ";? >
The code will be pre-formed a test.html, white is actually used space in exchange for time, such as the news site, the content of the release basically finished is not changed, more suitable for static into HTML.
Direct to this HTML page when the user accesses it.
Simply test the effect with Apache benchmark:
Ab.exe-n1000-c10
The two are at least 150ms apart in connection with more than 90%. Compared to the complexity of PHP, HTML saves a lot of computational capacity.
Introduction to additional related functions
1, Flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: 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
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()。
How to realize the static of large-scale PHP website? If you have tens of thousands of pages, do you want to regenerate all of them in one small article?
Using Libtemplate to generate static web pages
Author: iwind
Originally published in Dev-club an article, how to use the template processing program Phplib in the Template.inc to achieve static web page generation, hehe, incredibly be included in the essence, and was reproduced by a number of sites, want to think it is an honor. In fact, this aspect of the Internet a lot of things, I published the so-called Iams (Iwind article management System), there are, people can look. Below I just briefly summarize once.
Now generally say there are three ways to generate static Web pages, one is to configure the server, you can go to www.devarticles.com/c/b/PHP/to look for, for this many places have. The other one is to control the output with the Ob_ function. The method is as follows: First use Ob_start (), open the output buffer, then the analysis of the data, operation, and so on, followed by ob_get_contents (); Gets the contents of the buffer and then writes the file. According to this procedure, you can write the following program:
Ob_start ();
Body parts, data manipulation, processing, output, etc...
Require "global.php";
mysql_connect ("localhost", "root", "");
.....
Get buffer contents
$contents =ob_get_contents ();
If you do not want to output anything, you can add this sentence
Ob_end_clean ();
Write destination file
$fp = @fopen ($targetFile, "w+") or Die ("Error opening file");
Fwrite ($fp, $contents);
?>
This will write the content of this dynamic page to a static page, $targetFile. Like some Web site home page content, to invoke n multiple query statements, you may wish to generate static Web pages, not only greatly improve the access speed, but also reduce the burden of the server.
As you can see, I use ob_ just to handle a single page, which is not a method for bulk writing or updating multiple pages. That's the third way I'm going to talk about this, using templates. Template is a good thing, now we are more or less in use it, the proposal will not be simple template processing of netizens, take some time to learn it, the general template processing procedures are very simple. It is very simple to generate static Web pages with templates by getting the results of the analysis and writing the results to the file. Let's talk about the template.inc in Phplib if you use a template to generate a static web page.
One, modify Template.inc
Add several of the following functions:
Save the results of the analysis to a file
function SaveToFile ($dir, $varname) {
$data = $this, Finish ($this, Get_var ($varname));
$FP =fopen ($dir, "w+");
Fwrite ($fp, $data);
}
Clears an array of values that have been assigned
function Renew () {
$this v ... Remaining full text >>
How to implement dynamic page static in PHP system
Dynamic and static key to see what type of website you are, if the real-time is not high, similar to Sina News, he may change the number of times after the release may be less, such a site you can use to generate static HTML pages in a more appropriate way, if your page content is often changed, This time you can choose to use pseudo-static to achieve, the better way is to combine the cache, such as Memcache,redis cache system, can simultaneously reduce the load of the app and database server, why not? Pseudo-static benefits are beneficial to SEO optimization, can be obtained through the search engine to obtain higher rankings, thus obtaining more traffic.
http://www.bkjia.com/PHPjc/863319.html www.bkjia.com true http://www.bkjia.com/PHPjc/863319.html techarticle using OB series functions to implement the static PHP Web page, obphp PHP's execution page into HTML, is one of the so-called static PHP methods. There are other template substitution methods, Opcache, etc. ..