Today, many websites are static, and PHP can also easily implement static Publishing. There are many specific implementation methods. Let's talk about the two methods I will use. Method 1: ob_get_contents () This is a very convenient method and a very common method. The implementation principle is: first open the cache, then create the corresponding static page file and write it into the cache.
Today, many websites are static, and PHP can also easily implement static Publishing. There are many specific implementation methods. Let's talk about the two methods I will use. Method 1: ob_get_contents () This is a very convenient method and a very common method. The implementation principle is: first open the cache, then create the corresponding static page file and write it into the cache.
Today, many websites are static, and PHP can also easily implement static Publishing. There are many specific implementation methods. Let's talk about the two methods I will use.
Method 1: ob_get_contents ()
This is a convenient and frequently used method. The implementation principle is: first open the cache, then create the corresponding static page file, write the cached content, and clear the cache.
Example:
Ob_strart (); # Open the buffer $ Fn = date ('ymdhis '). rand (). 'html'; // generate a file name Require ("supply. php"); # load the file to generate the static page, because there is ob_clen () in the background, it will not be displayed $ Fs = fopen ($ fn, 'w'); # open a static page file Fwrite ($ fs, ob_get_contents (); # generate static files Ob_clean (); # Clear Cache ?> |
Method 2: file_get_contents ();
$ Fn = date ('ymdhis '). rand (). 'html '; $ Url = 'HTTP: // '. $ _ SERVER ['HTTP _ host']. "/"; # Note $ Content = file_get_contents ($ url ); $ Fs = fopen ($ fn, 'w '); Fwrite ($ fs, $ content ); ?> |
The following is an explanation of the above. If you only use a file name, not a URL, then if you use a reference file such as require in this file
('Header. php'); The content in header. php cannot be displayed.
Method 3: str_replace ()
$ Filemodel = "supply. php"; string 5 $ file = fopen ($ filemodel, "w + "); $ Temp = fread ($ file, filesize ($ filemodel )); $ Temp = str_replace ("[title]", $ title, $ temp ); $ Temp = str_replace ("[postTime]", $ postTime, $ temp ); $ Temp = str_replace ("[content]", $ content, $ temp ); ?> |
This method applies to simple pages. If supply. php uses a reference file such as require ('header. php'), the content in header. php cannot be displayed.
I just provided the simplest method. In practical applications, you can write a class that generates static pages and add corresponding exception handling to the class. This file only provides an example.