This article mainly introduces the PHP processing static page: Page Setup cache time graphic detailed, interested friends reference, I hope to help you.
Q: How do I trigger a system to generate a purely static page?
1. Page Add cache time
2. How to trigger manually
3.crontab timed Scan Program
Let's implement scenario one: Add cache time to page
User Request page =/page is out of date = = no (get static page) | | = = (Dynamic page generates a new static page)
if (if there is this static file && not expired) { //Get page}else{ //Regenerate a copy of static page}
OK, the basic logic is this, the following we refine the code:
<?phpif (Is_file ('./index.html ') && (Time ()-filemtime ('./index.html ') <) { //assuming the cache is 60 seconds// Get page require_once ('./index.html ');} else{ //Regenerate a copy of the static page//prepare the data to be displayed to the Web $data = Array ( ' id ' =>1, ' msg ' = ' Hello java '), Array (' ID ' =>2, ' msg ' = ' Hello php '), array (' ID ' =>3, ' msg ' = ' Hello python '), ); Render to template //actual project is usually rendered in HTML// demo here want to read Ob_start ();//Start Input buffer control foreach ($data as $item) { echo $item [' id ']. ' ===> '. $item [' msg ']. ' <br/> '; } Start generating a static paging file file_put_contents (' index.html ', ob_get_contents ());}
This way we access index.php, if the static file cache does not expire, its substance accesses the content from index.html this static file.