Common two methods for generating static html pages using php

Source: Internet
Author: User
: This article mainly introduces two common methods for generating static html pages using php. if you are interested in PHP tutorials, refer to them. Because each time a user clicks a dynamic link, the server sends a data query request.

For a website with millions of visitors, this is undoubtedly a huge burden on the server.

Therefore, converting dynamic data into static html pages is the first choice to save manpower and material resources.

Because we didn't have the relevant experience. at the beginning, we thought this technology was mysterious.

However, after reading some examples, I found that it was not that complicated (however, the information on the Internet is not particularly detailed)

After a morning trial in mid-afternoon, I finally completed the task. Here are some experiences and a simple example.

Hope the prawns don't joke

In general, there are two ways to reference the article about conversion and output of html pages using php:

First, use the template. At present, PHP templates can be said to be many, including powerful smarty and easy-to-use smarttemplate. Each of these templates has a function to get the output content. We use this function to generate static pages. The advantage of using this method is that the code is clear and readable.

Here I use smarty as an example to illustrate how to generate static pages:

  1. Require ("smarty/Smarty. class. php ");
  2. $ T = new Smarty;
  3. $ T-> assign ("title", "Hello World! ");
  4. $ Content = $ t-> fetch ("templates/index.htm ");
  5. // Here fetch () is the function used to obtain the Output content. in the $ content variable, it is the content to be displayed.
  6. $ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
  7. Fwrite ($ fp, $ content );
  8. Fclose ($ fp );
  9. ?>

Method 2: Use ob functions. The functions used here are mainly ob_start (), ob_end_flush (), ob_get_content (), where ob_start () indicates to open the browser buffer. after the buffer is enabled, all non-file header information from PHP programs is not sent, but stored in the internal buffer until you use ob_end_flush (). the most important function here is ob_get_contents (). The function is used to obtain the buffer content, which is equivalent to the fetch () above.

  1. Ob_start ();
  2. Echo "Hello World! ";
  3. $ Content = ob_get_contents (); // retrieves all the content output on the php page.
  4. $ Fp = fopen ("archives/2005/05/19/0001 .html", "w ");
  5. Fwrite ($ fp, $ content );
  6. Fclose ($ fp );
  7. ?>

The 2nd method I used is the ob series function.

When I first started reading this, I was a little confused. later I realized that ob is output buffering, that is, the output cache.

When you prepare the output, all the data is saved in the ob. after the server parses php, all the html code to be output to the client is stored in the ob. if we want to output the html static page you just need to extract the cache and write it into an html page.

So the principle is actually very simple.

Several functions are used here. I am not familiar with many php functions, so I hope to help you.

Ob_start (): starts "capturing" the cache, that is, starting from here to open the browser cache.

Ob_end_flush (): Disable browser cache

Ob_get_content (): Read cache content

Fopen ("file path", "Open mode") there are several main modes for opening the file function:

Open in "r" read-only mode and point the file pointer to the file header.

Open in r + read/write mode and point the file pointer to the file header.

Open in "w" writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.

Open the "w +" read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it.

Fwrite ("file name", "write content") write to file

Fclose () close the file

Because there may be several hundred html files to be converted, you cannot specify the fopen path statically. you can set a path variable to save user id and other information for html convenience. the following is a simple example of reading xml data from the last php file.

  1. Ob_start (); // open the browser cache
  2. // The following describes how to read xml data.
  3. $ Parser = xml_parser_create (); // Create a parser editor
  4. Xml_set_element_handler ($ parser, "startElement", "endElement"); // corresponding functions for tag setting triggering are startElement and endElenment respectively.
  5. Xml_set_character_data_handler ($ parser, "characterData"); // you can specify a function for reading data.
  6. $ Xml_file = "1.xml"; // specify the xml file to be read, which can be a url
  7. $ Filehandler = fopen ($ xml_file, "r"); // open the file
  8. While ($ data = fread ($ filehandler, 4096 ))
  9. {
  10. Xml_parse ($ parser, $ data, feof ($ filehandler ));
  11. } // Extract 4096 bytes each time for processing
  12. Fclose ($ filehandler );
  13. Xml_parser_free ($ parser); // Close and release the parser
  14. $ Name = false;
  15. $ Position = false;
  16. Function startElement ($ parser_instance, $ element_name, $ attrs) // function of the start tag event
  17. {
  18. Global $ name, $ position;
  19. If ($ element_name = "NAME ")
  20. {
  21. $ Name = true;
  22. $ Position = false;
  23. Echo "name :";
  24. }
  25. If ($ element_name = "POSITION ")
  26. {$ Name = false;
  27. $ Position = true;
  28. Echo "position :";
  29. }
  30. }
  31. Function characterData ($ parser_instance, $ xml_data) // function used to read data
  32. {
  33. Global $ name, $ position;
  34. If ($ position)
  35. Echo $ xml_data ."
    ";
  36. If ($ name)
  37. Echo $ xml_data ."
    ";
  38. }
  39. Function endElement ($ parser_instance, $ element_name) // function for ending a tag event
  40. {
  41. Global $ name, $ position;
  42. $ Name = false;
  43. $ Position = false;
  44. }
  45. // Xml data read is complete
  46. $ Htmlname = $ id. ". html"; // $ id can be defined here to represent the user's id
  47. $ Htmlpath = "archives/". $ htmlname; // set the path variable
  48. $ Content = ob_get_contents (); // retrieves all the content output on the php page.
  49. $ Fp = fopen ($ htmlpath, "w ");
  50. Fwrite ($ fp, $ content );
  51. Fclose ($ fp );
  52. ?>

Reprinted: http://www.cnblogs.com/awinlei/archive/2013/03/04/2942962.html

The above describes two methods to generate static html pages using php, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

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.