Home  >  Article  >  php教程  >  Talking about PHP generating static page analysis template + cache + writing files

Talking about PHP generating static page analysis template + cache + writing files

黄舟
黄舟Original
2016-12-14 13:24:341273browse

1. Introduction
In terms of speed, static pages are much faster than dynamic pages such as php. There is no doubt that static pages are less flexible. If you do not use a database or other devices to save relevant information, , the overall management is relatively cumbersome, such as modification and editing, such as reading permission restrictions, etc. However, for some files that we often use frequently, such as the news release system developed, we do not want many users to read the database before displaying the results. , On the one hand, this consumes the resources of the server, and on the other hand, it takes up a lot of valuable response time of the browser. Therefore, with the "static page" approach, many websites currently use this technology, usually managed by the administrator. Background control, or generating html for direct display, or using xhtml to control display with css, or generating xml for display with xslt. These techniques are not difficult. Here I will briefly talk about the method of generating html.
2. Preliminary knowledge
Template Technology:
[PHP] An in-depth introduction to the template engine Smarty--2005-12-31
[PHP] Talking about configuration, using Smarty technology--2006-01-04
Caching technology:
Some information, such as often unchanged, but still It is very valuable to put changeable information in the cache to speed up the display. The so-called cache, the popular understanding is some common information stored on the server side. It lives and dies with the server. When we save the cache You can specify the judgment of the next update time. For example, if you want to update once every 5 minutes, you can record the last update time and compare it with the current time. If it is greater than 5 minutes, read the database and update it. Otherwise, read the cached data directly. , 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)
Description: When the buffer is activated, all non-files from the PHP program No header information is sent, but is saved in an internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
Flush: refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
ob_get_contents: Returns the contents of the internal buffer.
Function format: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
ob_get_length: Returns the length of the internal buffer.
Function format: int ob_get_length(void)
Description: This function will return the length in the current buffer; like ob_get_contents, if the output buffer is not activated, it returns FALSE.
ob_end_clean: Delete the contents of the internal buffer and close it Internal buffer
Function format: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it
ob_end_flush: Send the contents of the internal buffer to the browser and close 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: Turns absolute flush on or off
Function format: void ob_implicit_flush ([int flag])
Description: The default is off Buffer, after turning on absolute output, each script output is sent directly to the browser, no longer need to call flush()
File writing:
int fwrite (resource handle, string string [, int length])
fwrite() Write the contents of string to the file pointer handle. If length is specified, writing will stop when length bytes have been written or when the string has been written, whichever occurs first.
fwrite() returns the number of characters written, or FALSE when an error occurs.
Related reference official website: File reference
3. Solution
Idea: turn on the ob_start buffer, get ob_get_contents when the data has been called out, then generate a static page, ob_end_clean clear the buffer. ok, that’s it, let’s look at an example ( Combination of php+mysql):
Create database:

Copy the code 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:

Copy the code 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 "Generate successfully $filename";
else
echo "Generate identification";
}
}

//Write the process of generating files as a 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. Notes
1: It is generally recommended that administrators add data When generating static pages, you can consider recording the generated file ranking and path.
2: PHP mainly uses ob_starts() and ob_get_contents, which are very useful when generating static pages. Of course, you can also consider calling out the database to directly replace the variables in the template. It is also possible.
3: It is possible to use smarty or phplib for the main template. Smarty is relatively easy to use.

I hope the above article content can help everyone. For more related articles, please pay attention to the PHP Chinese website (www.php.cn )!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn