PHP Automatic Update Generation home page

Source: Internet
Author: User
PHP Automatic Update Generation homepage
We know that access to dynamic pages is instantaneous from the server to read the data, when more and more visitors, the speed will be very slow, and the generated static page not only speed can solve the pressure of the server, but also for SEO search engine is very friendly.

Already very few people use dynamic direct reading data, and now the Web page is basically static HTML, the homepage can be said to be the most frequently visited pages, and of course the most updated frequency, then how to ensure that the home page is static HTML files, and can be updated in time to become a problem.

In the update summer article system, encountered this problem, how to trigger the user to automatically update the home page, first to understand the source, if only to update the first page for static HTML, then very simple, the use of PHP operation file function file_put_contents () Open the first page to read, and then use PHP operation of the function of the file to write, you can directly generate static HTML files, and this operation is done in the background, but the disadvantage is not to enable users to trigger the generation of static home page.

Below to clarify a thought, first generate static page has, then how to automatically update the homepage? In fact, automatically update the first page is to determine the time, in the WP system, there is automatically publish the article settings, such as the following code: if (Format_date (Time (), 1) > "20131001″" {echo "Automatic Update code";},20131001 Change to a certain regular time.

But I do not want to use the above method to achieve, need to pay attention to the matter, the first page is to use each update before the user to trigger the generation of static home page, in accordance with the above principle, then the code is very simple, to update the home page is completely enough.

$nowtime =time ();
$pastsec = $nowtime-$_get["T"];

if ($pastsec <30)
{
Exit 30 seconds update, time can adjust itself
}

Ob_start (); Open buffer
Include ("index.php"); Contains generated files, such as the home page
$content = Ob_get_contents (); Get the contents of the buffer zone
$content. = ""; Plus the code that calls the updater


File_put_contents ("index.html", $content); Write Build index.html

if (!function_exists ("file_put_contents"))//Read Write
{
function file_put_contents ($FN, $fs)
{
$FP =fopen ($FN, "w+");
Fputs ($fp, $FS);
Fclose ($FP);
}
}
echo Date ("y-m-d h:i:s"); Write Build time
echo "Static homepage successfully updated! "//generated successfully, can be deleted in actual application
?>

Run directly, you will find the home page below the "Current time, static first page successfully updated" text, then look at the site root directory, will be more than a index.html file, which is generated static HTML file. Open this file, you can see a section of the code: "" This is at any time t=1381743568 to generate static page handler f5.php, so that the next update, the user can simply open the static index.html page to automatically generate the homepage file.
This article fixed link: http://dfayl.com
  • 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.