PHP static page analysis template generation + cache + file writing

Source: Internet
Author: User
For PHP static page template generation + cache + file writing, refer to the code below. 1. Introduction
In terms of speed, static pages are much faster than dynamic pages, for example, php. this is without a doubt, but due to the poor flexibility of static pages, if you do not use a database or other devices to save relevant information, the overall management is cumbersome, such as modifying and editing. for example, read permission restrictions. However, we do not want many users to read the database to display the results in the developed news publishing system, in this way, on the one hand, it consumes server resources, and on the other hand it takes a lot of valuable response time for viewers. all websites currently use this technology with the "static page" approach, these technologies are generally controlled by the Management backend, html display is generated, html display is controlled by css, xml display is generated, and xslt display is used, here I will briefly introduce the html generation method.
2. prerequisites
Template technology:
[PHP] template engine Smarty introduction-
[PHP] use Smarty technology for configuration
Cache technology:
Some information remains unchanged for example, but can still be stored in the cache to speed up the display speed. this is very valuable. The so-called cache is commonly understood as shared information 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.
Reference Official website: document reference
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:

The code is as follows:


Create TABLE 'bihtml '(
'Id' int (11) not null auto_increment,
'Szdtitle' varchar (16) not null,
'Szdcontent' text not null,
Primary key ('id ')
) TYPE


Get the current ID and import the template:

The code is as follows:


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 (! Emptyempty ($ result ))
{
$ Tmp-> assign (array (
"Szdtitle", htmlspecialchars ($ result ['title']),
"Szdcontent", $ result ['title']);
}
$ Tpl-> display ('default _ 1. tpl ');
$ This_my_f = ob_get_contents (); // key here
Ob_end_clean ();
$ Filename = "$id.html ";
If (tohtmlfile_cjjer ($ filename, $ this_my_f ))
Echo "generated successfully $ filename ";
Else
Echo "generate recognition ";
}
}

// Write the function of the file generation process
Function tohtmlfile_cjjer ($ file_cjjer_name, $ file_cjjer_content)
{
If (is_file ($ file_cjjer_name )){
@ Unlink ($ file_cjjer_name );
}
$ Cjjer_handle = fopen ($ file_cjjer_name, "w ");
If (! Is_writable ($ file_cjjer_name )){
Return false;
}
If (! Fwrite ($ cjjer_handle, $ file_cjjer_content )){
Return false;
}
Fclose ($ cjjer_handle); // Close the pointer
Return $ file_cjjer_name;
}


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.

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.