Use php to generate static html pages (common 2 methods), static 2 _ PHP Tutorial

Source: Internet
Author: User
Use php to generate static html pages (two common methods) and two static methods. Use php to generate static html pages (common 2 methods ), static 2 because each time a user clicks a dynamic link, the server will send data query requests. for a single access, php can generate a static html page (common 2 methods ), static

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:

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.

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.

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

Callback (general 2 methods), static 2 because each time a user clicks a dynamic link, the server will send data query requirements for a single access traffic can reach...

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.