Home >Backend Development >PHP Tutorial >Two general methods to generate static html pages using php
Because every time a user clicks on a dynamic link, a data query request will be sent to the server
For a website with potentially millions of visits, this is undoubtedly a huge burden on the server
So convert the dynamic data Turning it into a static HTML page has become the first choice to save manpower and material resources
Because I had no corresponding experience before, I thought this technology was very mysterious at first
But after reading some examples, I found that it is not that complicated (but the information on the Internet is not Particularly detailed)
After a morning and afternoon experiment, I finally completed the task. Here are some thoughts and a simple example
I hope you guys won’t laugh at me
Generally speaking, use php to convert and output html pages. There are two ways to quote Da Xia’s article as follows:
The first way: use templates. There are currently many PHP templates, including the powerful smarty and the simple and easy-to-use smarttemplate. Each of their templates has a function to get the output content. The way we generate static pages is to use this function. The advantage of using this method is that the code is clearer and more readable.
Here I use smarty as an example to illustrate how to generate a static page:
Second method: Use the ob series of functions. The functions used here are mainly ob_start(), ob_end_flush(), ob_get_content(), where ob_start() means to open the browser buffer. After opening the buffer, all non-file header information from the PHP program will not be sent. Instead, it is stored in the internal buffer until you use ob_end_flush(). The most important function here is ob_get_contents(). The function of this function is to obtain the contents of the buffer, which is equivalent to the fetch() above. The reason the same.
The second type I chose The method is to use the ob series of functions
I was a little unclear when I first read this. Later I learned that ob is output buffering, which means output caching
When you are ready to output, all the data is saved in ob. After the server parses php, all the html codes to be output to the client are stored in ob. If we want to output an html static page, we only need to take out the cache and write it into an html page
So the principle is actually very simple
Here Several functions are used. Since I am new to PHP and I don’t understand many functions, I will explain them here. I hope it can help everyone
ob_start(): Start “capturing” the cache, which means opening the browser’s cache from here
ob_end_flush( ): Turn off the browser cache
ob_get_content(): Read the cached content
fopen("File path", "Open mode") Open the file There are several opening modes for this function. The following are the main modes:
"r" Open in read-only mode and point the file pointer to the file header.
"r+" Open in reading and writing mode and point the file pointer to the file header.
"w" opens in writing mode, points the file pointer to the file header and truncates the file size to zero. If the file does not exist, try to create it.
"w+" Open in reading and writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
fwrite("File name", "Write content") Write the file
fclose() Close the file
Since there are many html files I want to convert, there may be hundreds, so the path to fopen cannot be statically specified here. You can set a path variable in which you can save the ID and other information sent by the user to facilitate naming of HTML files. Here is a simple example of how I read xml data with PHP last time
Reprinted: http://www.cnblogs.com/awinlei/archive/2013/03/04/2942962.html
The above introduces two common methods for generating static html pages using PHP, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.