Home  >  Article  >  Backend Development  >  Source code and usage of static HTML generated by PHP_PHP tutorial

Source code and usage of static HTML generated by PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:54:44870browse

I recently studied some development technologies of PHP and found that PHP has many excellent functions that ASP does not have. It can complete some functions that could not be completed before, such as dynamically generating HTML static pages to reduce the load of the server CPU and improve user experience. Access speed.

We know that when PHP reads MYSQL for dynamic display, there will be many performance problems when the traffic is large. If you rent someone else's virtual host, the CPU will be restricted due to excessive CPU consumption, resulting in the web page becoming inaccessible. . Here I will give you a method for dynamically generating HTML in PHP, which can greatly reduce the server CPU load.

First set up the .htaccess file to convert dynamically called parameters into static HTML URL addresses. For example, forward the files in the post directory to the wp-post.php file in the root directory and add The statement is similar:

RewriteRule ^post/([a-z0-9-]+.html)$ wp-post.php?$1$2

Then modify the wp-post.php file, Add the following PHP code at the beginning of the file:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] ob_start();
$qstring = isset($_SERVER[%26quot;QUERY_STRING%26quot;]) ? $_SERVER[%26quot;QUERY_STRING%26quot;] : %26quot;%26quot;;
define(% 26quot;HTML_FILE%26quot;, $_SERVER['DOCUMENT_ROOT'].%26quot;/post/%26quot;.$qstring);

if (file_exists(HTML_FILE))
{
$ lcft = filemtime(HTML_FILE);
if (($lcft + 3600) %26gt; time()) // Determine whether the last generated HTML file is more than 1 hour ago. If not, directly output the file content
{
echo(file_get_contents(HTML_FILE));
exit(0);
}
}

After that is the existing PHP code, and then add the following PHP code at the end of the current code:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] define(%26quot;HTMLMETA%26quot;,%26quot;%26lt;!-- this is a real static html file created at %26quot;.date(%26quot;Y-m-d H:i:s%26quot;).%26quot ; --%26gt;%26quot;);
$buffer = ob_get_flush();
$fp = fopen(HTML_FILE, %26quot;w%26quot;);
if ($fp)
{
fwrite($fp, $buffer.HTMLMETA);
fclose($fp);
}

Okay, then check your static HTML page. If a comment line appears at the end of the page, it means that the static HTML file has been successfully created.

One application of this method is the WordPress Annual Blog Statistics Plug-in that I wrote earlier. This statistics plug-in queries the database more than ten times, so there will be big performance problems when many people access it. , after using the dynamic HTML generation technology I introduced, you can query once a day and generate statistical rankings, which perfectly solves the performance problem of querying the database.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364540.htmlTechArticleRecently researching some development technologies of PHP, I found that PHP has many excellent functions that ASP does not have, and it can complete some things that were not possible before. Completed functions, such as dynamically generating HTML static pages to reduce server...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn