PHP Generate static page note a few

Source: Internet
Author: User
Tags current time flush

One: Introduction
On the speed, static page than dynamic page for example PHP much faster, this is no doubt, but because the static page flexibility is poor, if not with the help of the database or other equipment to save the relevant information, the overall management is more cumbersome, such as modify editing. For example, read permission limits, but, Corresponding to some of the files we frequently use, for example, the development of the news release system, we do not want many users to read the database to display the results, which consumes the resources of the server, on the other hand, to take up a large number of visitors valuable response time, all, with the "static page words" approach, At present, many Web sites are using this technology, generally by the management of the background control, or generate HTML direct display, or XHTML with CSS control display, or generate XML in XSLT display, these techniques are not difficult, here I simply say the way to generate HTML.

II: Preparatory Knowledge

Template technology:


[PHP] Template engine Smarty Introduction--2005-12-31
[PHP] joke configuration, using Smarty technology 2006-01-04

Caching technology:

Some information, such as the constant, but still variable information in the cache to speed up the display speed, this is very valuable, the so-called cache, popular understanding is a number of stored on the server side of the common information. It's on the server, and we can specify the time of the next update when we save the cache, For example, to update in 5 minutes, you can record the last update time, and the current time comparison, if more than 5 minutes, read the database, update replaced, otherwise directly read the cached data, of course, the cache needs the client user activation, only once.

Ob_start () function: Opens the output buffer.
function format void Ob_start (void)
Note: When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use Ob_end_flush () or flush () to output the contents of the buffer.

Flush: Refreshes the contents of the buffer, output.
function format: Flush ()
Description: This function is frequently used and 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 FALSE if the output buffer is not active.

Ob_get_length: Returns the length of the internal buffer.
function format: int ob_get_length (void)
Description: 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: Deletes the contents of the internal buffer and closes 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])
Note: By default, the buffer is turned off, and when absolute output is turned on, each script output is sent directly to the browser and no longer needs to be invoked 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, when the length byte is written or a string is written, the write stops, depending on which case is first encountered.
Fwrite () returns the number of characters written and false when an error occurs.
Related Reference official website: File reference

III: Solutions

Train of thought: Open the Ob_start buffer, when the data has been transferred to get ob_get_contents, and then generate a static page, Ob_end_clean to clear the buffer. OK, that's it, let's take a look at an example (a combination of Php+mysql):
To create a database:

CREATE TABLE ' bihtml ' (
' id ' int (one) not NULL auto_increment,
' szdtitle ' varchar not NULL,
' szdcontent ' text not NULL,
PRIMARY KEY (' id ')
) Type=myisam;



Get the current ID and import the template:

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 [' 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 "Generation recognition";
}
}

Write out a function of the process of generating a file
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: Description of matters

1: Generally recommend that the administrator to add data when the static page generated, you can consider the record generated file ranking and path.

2:php is mainly Ob_starts () and ob_get_contents, generated static page is very useful, of course, you can also consider the database directly replace the template inside the variable is also possible.

3: The main template to use Smarty,phplib are possible, smarty use is relatively simple.

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.