Home > Article > Backend Development > Source code and usage of static HTML generated by PHP_PHP tutorial
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:
After that is the existing PHP code, and then add the following PHP code at the end of the current code:
Copy to ClipboardQuoted 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;);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.