Code _php Techniques for general PHP dynamic generation of static HTML Web pages

Source: Internet
Author: User
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 to include:

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:
Copy Code code 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 if the HTML file was last generated for more than 1 hours, if there is no direct output of the file content
{
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:
Copy Code code as follows:

Define (%26quot; htmlmeta%26quot;,%26quot;%26lt;! --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);
}

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%26ldquo I wrote earlier; WordPress Annual Blog Statistics plug-in%26rdquo, the statistical plug-ins because of the query 10 times database, many people visit the time 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.