Home >Backend Development >PHP Tutorial >General PHP dynamically generates code for static HTML web pages_PHP tutorial

General PHP dynamically generates code for static HTML web pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:40:28819browse

We know that when PHP reads MYSQL for dynamic display, there will be many performance problems when the number of visits is large. If you rent someone else's virtual host, the CPU will be restricted due to excessive CPU consumption, making the web page 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 the code The code is as follows:

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);
}
}

Following is the existing PHP code, and then add the following PHP code at the end of the current code:
Copy code The code is as follows:

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/321388.htmlTechArticleWe know that when PHP reads MYSQL for dynamic display, there will be many performance problems when the number of visits is large. If you rent someone else's virtual host, your CPU will be restricted due to excessive CPU consumption, leading to...
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