PHP implementation timed generation HTML Web site home

Source: Internet
Author: User
Tags current time

In order to improve the speed of the Web site, we often use to generate a static way to achieve, so that the site is really a lot of access to improve the speed. But there are some limitations to the timing generation, if you have a stand-alone server that can set up scheduled tasks on the server, However, if you are using a virtual host can be difficult to do. Although there are many ways. But it's easy to use, I think it's better to judge the difference between the generation time of the first file that has been generated and the time available, and it's easier to start generating this method if you meet a value. Don't say much.

Before you start, mention three functions: "Ob_start (), Ob_end_clean (), ob_get_contents ()"

Ob_start (): is to open the buffer, that is, you need to generate the contents of the static file cache here;
Ob_get_contents (): is read out of the buffer in the content, the following code for example;
Ob_end_clean (): This is more important, only after using this function, the contents of the buffer will be read out;

if (File_exists ("./index.htm"))//See whether the static index.htm file exists
{
$time =time ();

File modification time and current time difference? If so, direct the HTM file, or regenerate HTM
if ($time-filemtime ("./index.htm") < 600)
{
Header ("location:classhtml/main.htm");

}
}

Add Ob_start () at the beginning of your service;
Ob_start ();

Home content, is your dynamic part of the

Add Ob_end_clean () at the end and output this page to a variable
$temp =ob_get_contents ();
Ob_end_clean ();

Write to File
$FP =fopen ("./index.htm", ' W ');
Fwrite ($FP, $temp) or Die (' Write file error ');
echo "Generate HTML Finish!" ";



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.