PHP static release methods

Source: Internet
Author: User
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.

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.