2 ways to generate static HTML pages in PHP

Source: Internet
Author: User
Because every time the user clicks on the dynamic link, the server will send data query requests.

This is undoubtedly a big burden on the server for a website that can reach millions levels.

Therefore, the conversion of dynamic Data into static HTML page becomes the first choice to save manpower and material resources.

Because there was no corresponding experience at the beginning of the time that the technology is very mysterious

But after looking at some examples it is not that complicated (but the information on the Web is not particularly detailed)

After a morning plus mid-afternoon test, I finally got the job done. Here are some tips and a simple example

I hope the prawns don't laugh.

In general, there are two ways to use PHP to output HTML pages: The following are the articles that refer to prawns

The first: Use templates. PHP is now a template can be said to be many, there are powerful smarty, there are easy-to-use smarttemplate and so on. Each of these templates has a function to get the output content. The way we generate static pages is to take advantage of this function. The advantage of this method is that the code is clear and readable.

Here I use Smarty as an example to show 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 the fetch () is the function that gets the output content, now inside the $content variable, is to display the content
    6. $fp = fopen ("archives/2005/05/19/0001.html", "w");
    7. Fwrite ($fp, $content);
    8. Fclose ($FP);
    9. ?>

The second method: Use the functions of the OB series. The function used here is mainly Ob_start (), Ob_end_flush (), Ob_get_content (), where Ob_start () is the meaning of opening the browser buffer, when buffering is turned on, all non-file header information from the PHP program is not sent, but Save in the internal buffer until you use Ob_end_flush (). The most important function here is ob_get_contents (), the function of which is to get the contents of the buffer, equivalent to the fetch () above, the same reason.

    1. Ob_start ();
    2. echo "Hello world!";
    3. $content = Ob_get_contents ();//Get all the contents of the PHP page output
    4. $fp = fopen ("archives/2005/05/19/0001.html", "w");
    5. Fwrite ($fp, $content);
    6. Fclose ($FP);
    7. ?>

The 2nd method I've chosen is the function of the OB series.

When I first started looking at this, I was a little bit confused, and then I realized that OB was output buffering.

When you're ready to output, all the data is stored in the OB. after the server resolves PHP, all the HTML code to be output to the client is stored in the OB if we want to output an HTML static page just write the cache out to an HTML page

So the principle is actually very simple

Here's a couple of functions. Because I'm a beginner of PHP's many functions I don't know, so here's a little bit of hope to help you.

Ob_start (): Start capture cache that's where you open your browser's cache from here.

Ob_end_flush (): Close browser cache

Ob_get_content (): Read Cache contents

fopen ("File path", "open Mode") open a file There are several main modes of opening the function, which are described below:

The "R" read-only mode opens, pointing the file pointer to the file header.

The "r+" read-write mode opens, pointing the file pointer to the file header.

The "W" Write method opens, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.

The "w+" read-write mode opens, pointing the file pointer to the file header and truncating 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 file

Because I want to convert the HTML file very many may have hundreds of, so here can not statically specify the path of the fopen, you may set a path variable inside can save the user ID and other information to facilitate the HTML file name below is a simple example of my last PHP reading XML data

  1. Ob_start ();//Open Browser cache
  2. Here is the reading of XML data
  3. $parser = Xml_parser_create (); Create a parser editor
  4. Xml_set_element_handler ($parser, "startelement", "endElement");//Set the corresponding function when the tag is triggered here are startelement and endelenment respectively
  5. Xml_set_character_data_handler ($parser, "characterdata");//Set the corresponding function when reading data
  6. $xml _file= "1.xml";//Specifies the XML file to be read, which can be a URL
  7. $filehandler = fopen ($xml _file, "R");//Open File
  8. while ($data = Fread ($filehandler, 4096))
  9. {
  10. Xml_parse ($parser, $data, feof ($filehandler));
  11. }//4,096 bytes per fetch for processing
  12. Fclose ($filehandler);
  13. Xml_parser_free ($parser);//Close and Release parser parser
  14. $name =false;
  15. $position =false;
  16. function startelement ($parser _instance, $element _name, $attrs)//start tag event functions
  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)//functions when reading 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)//END tag Event
  40. {
  41. Global $name, $position;
  42. $name =false;
  43. $position =false;
  44. }
  45. XML Data Read complete
  46. $htmlname = $id. ". HTML ";//$id can define its own ID here on behalf of the user
  47. $htmlpath = "archives/". $htmlname; Set PATH variables
  48. $content = Ob_get_contents ();//Get all the contents of the PHP page output
  49. $fp = fopen ($htmlpath, "w");
  50. Fwrite ($fp, $content);
  51. Fclose ($FP);
  52. ?>

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

The above describes the use of PHP to generate static HTML page Common 2 methods, including the content of the contents, I hope that the PHP tutorial interested in a friend 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.