Static PHP pages: two static php static methods
Bytes ----------------------------------------------------------------------------------------------
Solution 1: If a static file exists and is generated within 30 seconds, the static page is directly returned (with time delay)
/*
| ------------------
| <Www. chenwei. ws>
| ------------------
*/
Header ('content-type: text/html; charset = UTF-8 '); $ id = $ _ GET ['id']? Intval ($ _ GET ['id']): ''; if ($ id ='') die ('enter the id of the news you want to query! '); $ Html_file = "news-id -". $ id. ". html "; // 1. main Code: if (file_exists ($ html_file) & filemtime ($ html_file) + 30> = time () {echo 'static page: '; echo file_get_contents ($ html_file ); exit;} // You can also use the DB tool class $ con = mysql_connect ('localhost', 'root', '123'); if (! $ Con) {die ('Connection failed! ');} Mysql_select_db ('testdb', $ con); $ SQL = "select * from bo_question where question_id = $ id"; $ res = mysql_query ($ SQL, $ con); if ($ row = mysql_fetch_assoc ($ res) {ob_start (); // 2. start the ob cache header ('content-type: text/html; charset = UTF-8 '); echo '
// Close the database connection (not required; if the connection is not persistent, the database will be closed automatically after the script is executed)
Mysql_close ($ con);} No resource found in else {echo! ';}
@ Blackeye poet
Solution 2: Use the template Replacement Technology (no time delay)
/*
| ------------------
| <Www. chenwei. ws>
| ------------------
*/
$ Tags = $ _ POST ['upload']; // add operation if ($ tags = 'add') {$ title = $ _ POST ['title']; $ content = $ _ POST ['content']; // If you strictly follow MVC, the model should be called here $ con = mysql_connect ('localhost', 'root ', '20140901'); if (! $ Con) {die ('Connection failed! ');} Mysql_select_db ('News', $ con); $ SQL = "insert into question (null,' $ title', '$ content ','')"; if (mysql_query ($ SQL, $ con) {// 1. generate a static file $ id = mysql_insert_id (); $ html_filename = 'news-id'.$id.'html '; $ html_fp = fopen ($ html_filename, 'w'); // 2.read the template file (news.html) $ fp = fopen ('news. tpl ', 'R'); // r read-only mode; r + read/write mode; w write mode: file content will be cleared! If the file does not exist, it will be created; a will be opened as an append // 3. Read the file cyclically // if the last part of the file is not read, it will always read while (! Feof ($ fp) {// read a row $ row = fgets ($ fp ); // Replace the placeholder => you can customize the complete replacement rule function $ row = str_replace ('% title %', $ title, $ row ); // If $ row is not re-assigned, the $ row value will not change $ row = str_replace ('% content %', $ content, $ row); fwrite ($ html_fp, $ row); // 4. write content to a static file}
// 5. The file must be fclose ($ html_fp); fclose ($ fp); echo "added successfully. <A href = 'newslist. php'> click to view the news! </A> ";} else {die ('failed to add! ') ;}} // Click the link to view details in the News list. You can change it to the generated static page address to directly access the static file. // News. tpl template file/*
Bytes ---------------------------------------------------------------------------------------------
Php Static Page method, which can also be pseudo-static
Here is the static member list in jetee.cn. According to this code, you can understand some static concepts.
Static Functions
/**
* @ Get list member item get the content of the variable to be replaced in the static template.
* @ Param str: To replace the string
* @ Return string
*/
Function get_staticize_replace_str ()
{
$ Replace_str = ""; // replace string
$ Query = "select member_id, email_name from member ";
$ Result = Mysql: query ($ query );
While ($ row = Mysql: fetch_assoc ($ result ))
{
$ Replace_str. = "<li>". $ row ["member_id"]. "</li> ";
$ Replace_str. = "<li>". $ row ["email_name"]. "</li> <br> ";
}
Return $ replace_str;
}
/**
* @ Replace the variables in the static template to generate a static page.
*@
* @ Return void
*/
Function staticize_list_member ()
{
$ Replace_str = $ this-> get_staticize_replace_str ();
$ Templet = fopen (TENDAO_DIR. "/templets/default/list_member.html", "r ");
$ New_file = fopen (TENDAO_DIR. "/member/list_member.html", "w ");
While (! Feof ($ templet ))
{
$ Ripe = fgets ($ templet );
$ Ripe = str_replace ("{member_items}", $ replace_str, $ ripe );
Fwrite ($ new_file, $ ripe );
}
If (file_exists (TENDAO_DIR. "/member/list_member.html ")){
Msg ("the static member list is successful! Back to homepage ...... ", TENDAO_ROOT, 0,3000 );
Exit ();
} Else {
Msg ("the static member list is not successful! Back to homepage ...... ", TENDAO_ROOT, 0,3000 );
}
Fclose ($ templet );
Fclose ($ new_file );
}
/**
* @ Static template
*@
... The remaining full text>
Php issues about page static
Many methods
Use include_once in php.
Only when the file is included, note that the file you split is only part of <body> </body>.
Then look at the page style
Good programmers will separate many parts
This facilitates unified management.