Dynamic | tutorials | web | static
Recent research on the development of PHP technology, found that PHP has a lot of ASP does not have the outstanding features, can complete some previously unable to complete the function, such as dynamically generated HTML static page, to reduce the server CPU load, improve user access speed.
We know that PHP read MySQL dynamic display, in the case of large access, there will be a lot of performance problems, if the rental of other people's virtual host, will be due to excessive CPU consumption is limited CPU, resulting in web pages can not be accessed. I am here to give a PHP dynamic generation of HTML method, can greatly reduce the server CPU load.
First set up the. htaccess file to convert the dynamically invoked parameter to a static HTML URL address, such as a file that will be in the post directory, forwarded to the wp-post.php file in the root directory, and a similar statement: Rewriterule ^post/([ a-z0-9\-]+\.html) $ wp-post.php?$1$2
Then modify the wp-post.php file and add the following PHP code at the beginning of the file:
The following is a reference fragment: Ob_start (); $qstring = Isset ($_server["query_string"])? $_server["Query_string"]: ""; Define ("Html_file", $_server[' Document_root '). " /post/". $qstring); if (file_exists (html_file)) { $LCFT = Filemtime (html_file); if ($LCFT + 3600) > Time ())//Determine if the last generation of HTML file is more than 1 hours, if not to output directly the contents of the file { Echo (file_get_contents (html_file)); Exit (0); } } |
Then there is the existing PHP code, followed by the following PHP code at the end of the current code:
The following is a reference fragment: Define ("Htmlmeta", "<!--this are a real static HTML file created at". Date ("Y-m-d h:i:s"). "-->"); $buffer = Ob_get_flush (); $fp = fopen (html_file, "w"); if ($FP) { Fwrite ($FP, $buffer. Htmlmeta); Fclose ($FP); } |
OK, then look at your static HTML page, and if there's a comment line at the end of the page, you've successfully created a static HTML file.
One application of this method is the "WordPress Annual Blog Statistics Plugin" that I wrote earlier, this statistical plug-in because query 10 times database, many people visit will have a lot of performance problems, using my introduction of this dynamic generation of HTML technology, one day to query, generate a ranking of statistics, solves the query database performance problem perfectly.