Common PHP dynamic generation of static HTML webpage code

Source: Internet
Author: User
Recently I have studied some PHP development technologies and found that PHP has many excellent functions not available in ASP, which can be used to complete some previously unfinished functions, such as dynamic generation of HTML static pages, to reduce the server CPU load and improve the user access speed. We know that PHP reads MYSQL dynamically and shows that there will be a lot of performance problems when traffic is high. if someone else's virtual host is rented, the CPU will be limited due to excessive CPU consumption, the webpage cannot be accessed. Here is a PHP Dynamic HTML generation method, which can greatly reduce the server CPU load.

First set. htaccess file, convert Dynamic Call parameters to static html url address, for example, the file under the post Directory, forward to the wp-post.php file in the root directory, the added statement is similar:

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 code is as follows:


Ob_start ();
$ Qstring = isset ($ _ SERVER [% 26 quot; QUERY_STRING % 26 quot;])? $ _ SERVER [% 26 quot; QUERY_STRING % 26 quot;]: % 26 quot; % 26 quot ;;
Define (% 26 quot; HTML_FILE % 26 quot;, $ _ SERVER ['document _ root']. % 26 quot;/post/% 26 quot;. $ qstring );

If (file_exists (HTML_FILE ))
{
$ Lcft = filemtime (HTML_FILE );
If ($ lcft + 3600) % 26gt; time () // checks whether the last HTML file was generated for more than 1 hour. if not, the file content is output directly.
{
Echo (file_get_contents (HTML_FILE ));
Exit (0 );
}
}


Then there is the existing PHP code, and the following PHP code is added at the end of the current code:

The code is as follows:


Define (% 26 quot; HTMLMETA % 26 quot;, % 26 quot; % 26lt ;! -- This is a real static html file created at % 26 quot ;. date (% 26 quot; Y-m-d H: I: s % 26 quot ;). % 26 quot; -- % 26gt; % 26 quot ;);
$ Buffer = ob_get_flush ();
$ Fp = fopen (HTML_FILE, % 26 quot; w % 26 quot ;);
If ($ fp)
{
Fwrite ($ fp, $ buffer. HTMLMETA );
Fclose ($ fp );
}


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

One application of this method is the previously written % 26 ldquo; WordPress annual blog statistics plug-in % 26 rdquo;. this statistics plug-in is used to query more than 10 databases, A lot of people may have a lot of performance problems when accessing. after using the dynamic HTML generation technology I introduced, I will query it once a day and generate a statistical ranking, it perfectly solves the problem of database query performance.

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.