PHP generate static HTML source code and Usage _php tutorial

Source: Internet
Author: User
Recently, some of the development techniques of PHP, found that PHP has a lot of ASP does not have the excellent features, can do some previously unable to complete functions, such as dynamic generation of HTML static pages, to reduce server CPU load, improve user access speed.

We know that PHP read MySQL dynamic display, in the case of large traffic, there will be a lot of performance problems, if renting someone else's virtual host, it will be due to excessive CPU consumption and the CPU is limited, resulting in web pages inaccessible. Here's a way for PHP to dynamically generate HTML, which can greatly reduce the CPU load on the server.

First, set the. htaccess file, convert the dynamically called parameters to the static HTML URL address, for example, the file in the Post directory, forward to the root directory of the wp-post.php file, join the statement similar to:

Rewriterule ^post/([a-z0-9\-]+\.html) $ wp-post.php?$1$2

Then modify the wp-post.php file to include the following PHP code at the beginning of the file:

Copy to ClipboardWhat to refer to: [www.bkjia.com]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 last HTML file was generated more than 1 hours, and if not, output the file contents directly
{
Echo (file_get_contents (html_file));
Exit (0);
}
}

After that is the existing PHP code, and then add the following PHP code at the end of the current code:

Copy to ClipboardWhat to refer to: [www.bkjia.com]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, if a comment line appears at the end of the page, indicating that a static HTML file has been created successfully.

One application of this method is the one I wrote earlier%26ldquo; WordPress Annual Blog Statistics plug-in%26rdquo; This statistical plug-in due to query more than 10 database, many people visit when there will be a lot of performance problems, using the dynamic generation of HTML technology I introduced, one day query, generate a statistical ranking, Perfectly solves the performance problem of querying the database.

http://www.bkjia.com/PHPjc/364540.html www.bkjia.com true http://www.bkjia.com/PHPjc/364540.html techarticle recently, some of the development techniques of PHP, found that PHP has a lot of ASP does not have the excellent features, can complete some previously unable to complete functions, such as dynamic generation of HTML static pages, to reduce the service ...

  • 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.