Talk about PHP generate static page analysis template + cache + Write file _php tutorial

Source: Internet
Author: User
First, Introduction
In the speed, static page than dynamic page analogy PHP much faster, there is no doubt, but because the static page flexibility is poor, if not with the use of database or other equipment to save relevant information, the overall management is more cumbersome, such as modify the editor. For example, read permission restrictions, but Corresponding to some of our frequently used files, for example, the development of the press release system, we do not want many users to read the database to display results, so that on the one hand consumes the resources of the server, on the other hand accounted for a large number of visitors to the valuable response time, all, with the "static page" approach, At present, many Web sites use this technology, generally by the management of the background control, or generate HTML direct display, or XHTML with CSS control display, or generate XML with XSLT display, these techniques are not difficult, here I would like to talk about the method of generating HTML.
II. Preparation of knowledge
Template technology:
[PHP] Template engine Smarty Introduction--2005-12-31
[PHP] joke configuration, using Smarty technology--2006-01-04
Caching technology:
Some of the information is often constant, but still can change the information in the cache to speed up the display speed, which is very valuable, so-called cache, popular understanding is some saved on the server side of the common information. It is in the server with life and death, we can specify the time of the next update when saving the cache, For example, to update in 5 minutes, you can record the time of the last update, compared with the current time, if more than 5 minutes, read the database, update replaced, otherwise directly read the cached data, of course, the cache requires the client user activation, only once.
Ob_start () function: Opens the output buffer.
function format void Ob_start (void)
Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
Flush: Refreshes the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
Ob_get_contents: Returns the contents of the internal buffer.
function Format: string ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
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, and, like ob_get_contents, returns FALSE if the output buffer is not active.
Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
function format: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it
Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer
function format: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any)
Ob_implicit_flush: Turn absolute refresh on or off
function format: void Ob_implicit_flush ([int flag])
Description: The default is to close the buffer, after opening the absolute output, each script output is sent directly to the browser, no longer need to call flush ()
File write:
int fwrite (resource handle, string string [, int length])
Fwrite () writes the contents of the string to the file pointer handle. If length is specified, the write stops when the length byte is written or after the string is written, depending on which situation is encountered first.
Fwrite () returns the number of characters written, or False if an error occurs.
Related References official website: File reference
Third, the solution
Idea: Turn on Ob_start buffering, get ob_get_contents when the data is already paged out, then generate a static page, Ob_end_clean clear the buffer. OK, so come on, take a look at an example (Php+mysql's combination):
To create a database:
Copy CodeThe code is as follows:
Create TABLE ' bihtml ' (
' id ' int (one) not NULL auto_increment,
' Szdtitle ' varchar (+) not NULL,
' Szdcontent ' text is not NULL,
PRIMARY KEY (' id ')
) TYPE

Get the current ID and import the template:
Copy CodeThe 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 [' titles ']),
"Szdcontent", $result [' titles ']);
}
$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 "Build success $filename";
Else
echo "Generate recognition";
}
}

Write the process of generating a file out of function
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 pointer
return $file _cjjer_name;
}

Iv. Matters of description
1: Generally recommended when the administrator to add data to generate static pages, you can consider the record generated file rank and path.
2:php is mainly Ob_starts () and ob_get_contents, the time to generate static pages is useful, of course, you can also consider to bring up the database directly replace the variables inside the template is also possible.
3: The main template using Smarty,phplib are available, Smarty use is relatively simple.

http://www.bkjia.com/PHPjc/320371.html www.bkjia.com true http://www.bkjia.com/PHPjc/320371.html techarticle first, the introduction in the speed, static page than dynamic page analogy PHP much faster, this is no doubt, but because the static page flexibility is poor, if not with the help of database or other set ...

  • 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.