PHP automatically updates and generates the home page

Source: Internet
Author: User
PHP automatically updates and generates the home page. we know that accessing dynamic pages reads data from the server in real time. when there are more and more visitors, the speed will be slow, the generated static pages are not only fast, but also friendly to SEO search engines. Few people use dynamic and direct data reading. Currently, static html is basically used on webpages. the homepage of a website can be said to be the most frequently accessed page, and of course the most frequently updated page, so how does PHP automatically update and generate a home page?
We know that accessing dynamic pages is to read data from the server in real time. when there are more and more visitors, the speed will be slow, and the generated static pages will not only solve the server pressure problem, it is also very friendly for SEO search engines.

Few people use dynamic and direct data reading. Currently, static html is basically used on webpages. the homepage of a website can be said to be the most frequently accessed page, and of course the most frequently updated page, so how to ensure that the home page is a static html file and can be updated in a timely manner becomes a problem.

This problem occurs when updating the summer article system. how to trigger the user's automatic homepage update? First, let's take a look at the source. if you only want to update the homepage as static html, it is very simple, use the file_put_contents () function of the php file operation to open the home page for reading, and use the function of the php file operation to write data, you can directly generate a static html file, which is performed in the background, however, the disadvantage is that the user cannot be triggered to generate a static homepage.

Next, let's clarify one idea. first, a static page is generated. how can we automatically update the homepage? In fact, the automatic update of the homepage is the judgment time. in the wp system, there is an automatic Post setting, for example, the following code: if (format_date (time (), 1)> "20131001 ″) {echo "automatically update code";}, 20131001 can be changed to a regular time.

However, I do not want to use the above method to implement it. I need to pay attention to the fact that this homepage is generated by using the access user before each update to trigger the generation of the static homepage. according to the above principle, the code is very simple. it is enough to update the homepage.

$ Nowtime = time ();
$ Pastsec = $ nowtime-$ _ GET ["t"];

If ($ pastsec <30)
{
Exit; // updated once every 30 seconds. the time can be adjusted by yourself.
}

Ob_start (); // open the buffer
Include ("index. php"); // contains the generated file, such as the homepage.
$ Content = ob_get_contents (); // Obtain the buffer content.
$ Content. =" T = ". $ nowtime." \ "> script"; // add the code that calls the update program.


File_put_contents ("index.html", $ content); // write to generate index.html

If (! Function_exists ("file_put_contents") // read and 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 generation time
Echo "the static homepage is successfully updated! "// Generated successfully. it can be deleted in actual applications.
?>

Run the command directly. you will find the text "Current Time, static homepage updated successfully" appears on the homepage page. then, you can view the index.html file under the root directory of the website, this is the generated static html file. Open this file and you will see an additional code:" T = 1381743568 "> script". this is to pass the time t = 1381743568 to the processing program f5.php that generates the static page at any time. in this way, the next time you update the page, you can open the static index.html page to automatically generate the homepage file.
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.