Update three ways to generate a purely static page: 1. Update by time interval. 2. Manual update. 3. Timed updates (System mates required).
1. Update by time interval.
When the user first accesses this page, the program automatically determines whether the static file exists, and if the file is still in the valid time, if the file does not exceed the valid time, the user accesses the static file that has been generated. If the validity time is exceeded, the user gets the content of the dynamic output while regenerating the static file. Just a little bit of modification of yesterday's code as an example:
<?php//first determine if there is a static file, and whether the latest modification of the file is now greater than 20 seconds if (Is_file ('./text.php ') && time ()-filemtime ('./text.php ') <= 20) {//condition established, output of the generated static file to user include_once ('./text.php ');} else{//Condition not set, re-walk the database, get data, assign template, output content $arr=array (0=>array (title=> ' Here is the first title '), 1=>array (title=> ' Here is the second title '), 2=>array (title=> ' Here is the third title '), 3=>array (Title=>date (' y-m-d h:i:s ')); Buffers must be turned on Ob_start () before output,//Output template include_once ('./template.php '); Generate static files at the same time as the output, note that the function used is ob_get_contents (); File_put_contents ('./text.php ', ob_get_contents ());/*if (file_put_ Contents ('./text.php ', ob_get_contents ()) {echo ' Success ';} Else{echo ' ERROR ';} */}?>
2. Manually update static files
This super simple, make a button, give it a <a> tag, link to this script, click the button, generate a new static file, the logic code and written yesterday exactly the same, for the sake of reading convenience, I still copy yesterday's code to come over.
<?php//According to conventional routines, this array should be taken from the database//here to prepare a two-bit array $arr=array (0=>array (title=> ' Here is the first title ') for simplicity, 1=>ar Ray (title=> ' Here is the second headline '), 2=>array (title=> ' Here is the third title ')); The buffer must be opened Ob_start () before the output,//The template file is introduced into the include_once ('./template.php '); Get the file from the buffer, write to the locally saved if (File_put_contents ('./text.php ', Ob_get_clean ())) {echo ' Success ';} Else{echo ' error ';}? >
3. Timed updates (this requires a Linux system)
Command line CRONTAB-E Edit crontab
*/1 * * * * * php/index.php execute index.php script every minute
Five * respectively: time-sharing week
View the crontab log to query whether static files are generated. Command: Tail-f/var/log/cron.log
Small Ant Learning page static (2)--Update three ways to generate a purely static page