PHP dynamically generate static HTML Web tutorials

Source: Internet
Author: User
Tags define html page php file php code query reference root directory
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.



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.