How PHP generates a static page

Source: Internet
Author: User
Tags exit empty functions html page php file php website

In the development of PHP Web site for the promotion and SEO needs, need to site for the whole station or local static processing, PHP generated static HTML page has a variety of methods, such as the use of PHP templates, caching, such as the implementation of the page static, today in the PHP instance tutorial form to discuss the method of generating static pages PHP. Page static method, divided into two kinds, one is pseudo static, is URL rewrite, one is you really static. Here's a description of how the page is static in PHP.

What is PHP static

The simple understanding of PHP static is to make the site generated page in the form of static HTML display in front of visitors, PHP static separation of pure static and pseudo static, the difference between the two is that PHP generated static page processing mechanism is different.

How PHP generates a static HTML page

1, the use of PHP templates to generate static pages

PHP templates to achieve static is very convenient, such as the installation and use of PHP Smarty implementation of the Web site static.

2, using PHP file read and write function to generate static page

PHP generates static page instance code

3, use the PHP output control function (output controls) to generate a static page

Output control functions, which use and control caching to generate static HTML pages, also use the PHP file read-write function.

PHP generates static page instance code

We know that the use of PHP for Web site development, the general results directly output to the browser, in order to use PHP to generate static pages, you need to use the output control function to control the buffer cache to obtain the contents of the buffer, and then output to the static HTML page file to achieve static Web site.

PHP generated a static page thinking: First open the cache, and then output the HTML content (you can also include HTML content in the form of file), and then get the contents of the cache, empty the cache through the PHP file read and write function to write the cached content to the static HTML page file. PHP file reading and writing tutorial?

The process of getting the cached content of the output to generate a static HTML page requires three functions: Ob_start (), ob_get_contents (), Ob_end_clean ().

Knowledge Points:

1, Ob_start function is generally used to open the cache, pay attention to the use of Ob_start can not have any output, such as spaces, characters and so on.

2, the Ob_get_contents function is mainly used to get the contents of the cache to return as a string, note that this function must be called before the Ob_end_clean function, otherwise the cached content is not obtained.

3, the Ob_end_clean function is mainly empty the contents of the cache and turn off the cache, success returns TRUE, Failure returns false

There are many applications for PHP output control functions, which will be launched in the future.

At this point, the use of PHP to generate static HTML page to achieve static site method is introduced, according to the actual situation and requirements you can choose different static method.

  1. ?
  2. $out 1 = "
  3. <body> Welcome to visit the PHP website Development Tutorial Network www.leapsoul.cn, this article mainly introduces the PHP Web page static method
  4. </body>
  5. $fp = fopen ("leapsoulcn.html", "w");
  6. if (! $fp)
  7. {
  8. echo "System Error";
  9. Exit ();
  10. }
  11. else {
  12. Fwrite ($fp, $out 1);
  13. Fclose ($FP);
  14. echo "Success";
  15. }
  16. ?>
    1. ?
    2. Ob_start ();
    3. echo "
    4. "
    5. "<title>php website static tutorial </title>".
    6. "
    7. "<body> Welcome to visit the PHP website Development Tutorial Network www.leapsoul.cn, this article mainly introduces the PHP Site page static method </body>".
    8. "
    9. $out 1 = ob_get_contents ();
    10. Ob_end_clean ();
    11. $fp = fopen ("leapsoulcn.html", "w");
    12. if (! $fp)
    13. {
    14. echo "System Error";
    15. Exit ();
    16. }
    17. Else
    18. {
    19. Fwrite ($fp, $out 1);
    20. Fclose ($FP);
    21. echo "Success";
    22. }
    23. ?>


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.