Method for regularly updating static pages

Source: Internet
Author: User

Implementation ideas:
Write a program m. php uses ob to call network. php output (if network. php as the homepage), save the output content to index.html, and add the call m. php code (<script language = javascript src = m. php? T = ". $ nowtime. "> </script>, t is the update time. You can determine whether the update time is reached in the next update.) The first time you manually execute m.phpto generate index.html, m.phpwill be automatically used to update index.html in the future, so as to automatically update the static homepage code:
Java code
Try {
<?
$ Nowtime = time ();
$ Pastsec = $ nowtime-$ _ GET ["t"];
If ($ pastsec <300 ){
Exit (); // update once every 5 minutes. The time can be adjusted by yourself.
}
Ob_start (); // open the buffer
Include ("network. php ");
$ Content = ob_get_contents (); // obtain the buffer content.
$ Content. = "\ n <script language = javascript src = \" m. php? T = ". $ nowtime." \ "> </script>"; // Add the code that calls the update program.
 
File_put_contents ("index.html", $ content );
 
If (! Function_exists ("file_put_contents ")){
Function file_put_contents ($ fn, $ fs ){
$ Fp = fopen ($ fn, "w ");
Fputs ($ fp, $ fs );
Fclose ($ fp );
}
}
?>
}
Catch (e ){};
Method 2:
PHP regularly generates the HTML website homepage
Before you start, let's mention three functions: "ob_start (), ob_end_clean (), ob_get_contents ()"
Ob_start (): it is used to open the buffer, that is, to cache the content of the static file you need to generate here;
Ob_get_contents (): Read the content in the buffer. The following code is used as an example;
Ob_end_clean (): This is important. Only when this function is used can the content in the buffer be read;
// Is the file modification time different from the current time? Otherwise, the htm file will be generated again.
Java code
If (file_exists ("./index.htm") // check whether the static index.htm file exists.
{
Time = time ();
// Is the file modification time different from the current time? Otherwise, the htm file will be generated again.
If (time-filemtime ("./index.htm") <600)
{
Header ("Location: classhtml/main.htm ");
}
}
// Add ob_start () to your start ();
Ob_start ();
// The homepage content is your dynamic part
// Add ob_end_clean () to the end and output this page to a variable.
Temp = ob_get_contents ();
Ob_end_clean ();
// Write a file
Fp = fopen ("./index.htm", ''w '');
Fwrite (fp, temp) or die (''file write error '');
// Echo "HTML generation complete! ";
FSO is short for FileSystemObject or Scripting. FileSystemObject. It is a built-in IIS component used to operate disks, folders, or text files. FSO has many objects, methods, and attributes. FSO judges the last update time of the file, and then selects whether to update the file based on the time value.
First, write a function to obtain the time for easy calling.
Java code
Function GetDateLast (path)
On error resume next
Dim FSO, objFile
Set FSO = Server. CreateObject ("scripting. filesystemobject ")
GetDateLast = FSO. GetFile (Server. Mappath (path). DateLastModified
Set FSO = Nothing
If err. number <> 0 then err. clear
End Function
Then the time is compared,
Java code
DateDiff ("n", CDate (GetDateLast ("file path"), Now ())

Author: "hudeyong926"
 

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.