Php generates static html

Source: Internet
Author: User
I. Introduction as we all know, html pages are faster than directly executing php pages, because html is directly transmitted to the request end without the need for the server to execute script programs, this not only greatly reduces the pressure on servers and databases, especially when there are a large number of visitors, but also brings a better user experience and is also conducive to the analysis and indexing of search engines.

1. As we all know, html pages are faster than directly executing php pages, because html is directly transmitted to the request end without the need to execute script programs on the server, this not only greatly reduces the pressure on servers and databases, especially when there are a large number of visitors, but also brings a better user experience and is also conducive to the analysis and indexing of search engines.

1. Introduction

As we all know, html pages are faster than directly executing php pages, because html is directly transmitted to the request end without the need to execute script programs on the server, which not only greatly reduces the pressure on servers and databases, especially when there are a lot of visitors, it is even more obvious. It also brings a better user experience and is also conducive to the analysis and indexing of search engines, with many advantages. This article is mainly about How to Use PHP to generate html.


2. Prerequisites

Cache Technology:
Some information may need to remain unchanged frequently, but the information that can be changed is stored in the cache to speed up display. This is very valuable, in general, some shared information is stored on the server. it is generated on the same server. When saving the cache, we can specify the next update time. For example, we need to update it once every five minutes to record the last update time, compared with the current time, if it is more than 5 minutes, read the database, replace the update with, otherwise directly read the cache data, of course, the cache needs to be activated by the client user, only once.

Ob_start () function: Open the output buffer.
Function Format void ob_start (void)
Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.
Flush: refresh the buffer content and output it.
Function Format: flush ()
Note: This function is frequently used and highly efficient.
Ob_get_contents: returns the content of the internal buffer.
Function Format: string ob_get_contents (void)
Note: This function returns the content in the Current Buffer. If the output buffer is not activated, FALSE is returned.
Ob_get_length: returns the length of the internal buffer.
Function Format: int ob_get_length (void)
Note: This function returns the length of the Current Buffer. It is the same as ob_get_contents. If the output buffer is not activated, FALSE is returned.
Ob_end_clean: Delete the content of the internal buffer and disable the internal buffer.
Function Format: void ob_end_clean (void)
Note: This function will not output the content of the internal buffer but delete it.
Ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.
Function Format: void ob_end_flush (void)
Description: This function sends the content of the output buffer (if any)
Ob_implicit_flush: Enables or disables absolute refresh.
Function Format: void ob_implicit_flush ([int flag])
Note: the buffer zone is disabled by default. After absolute output is enabled, the output of each script is directly sent to the browser, and you do not need to call flush ()

File writing:
Int fwrite (resource handle, string [, int length])
Fwrite () writes the string content to the handle of the file pointer. If length is specified, the write will stop after the length byte is written or the string is written.
Fwrite () returns the number of written characters. If an error occurs, FALSE is returned.


Iii. Solutions
Idea: Enable the ob_start buffer. When data has been called up, Get ob_get_contents and generate a static page. The ob_end_clean clears the buffer. OK. Let's look at an example (combination of php and mysql ):
Create a database:
Create table 'bihtml '(
'Id' int (11) not null auto_increment,
'Szdtitle' varchar (16) not null,
'Szdcontent' text not null,
Primary key ('id ')
) TYPE = MyISAM;
Get the current ID and import the template:


Iv. Instructions
1: it is generally recommended that the Administrator generate a static page when adding data, and record the generated file name times and paths.
2: php is mainly ob_starts () and ob_get_contents, which are useful when generating static pages. Of course, you can also consider calling up the database to replace the variables in the template directly.
3: The main templates use smarty and phplib, which are easy to use.


Ob_start (); $ id = _ POST ['id'] if (! Isset ($ id) & is_integer ($ id) {@ $ db = new mysqli ('localhost', 'root', 'admin', 'bihtml '); $ result = $ db-> fetch_one_array ("select * from szd_bi where id = '$ id'"); if (! Empty ($ result) {$ tmp-> assign (array ("Szdtitle", htmlspecialchars ($ result ['tidles ']), "Szdcontent ", $ result ['tidles ']);} $ tpl-> display ('default _ 1. tpl '); $ this_my_f = ob_get_contents (); // here the key ob_end_clean (); $ filename = "$id.html"; if (tohtmlfile_cjjer ($ filename, $ this_my_f )) {echo "generated successfully $ filename";} else {echo "generated for recognition ";}}

This article is excerpted from php to generate static html

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.