Solution: Use the template replacement technology (no time delay)
The code is as follows: |
Copy code |
/* | ------------------ | <Www.111cn.net> | ------------------ */ $ Users = $ _ POST ['users']; // add operation If ($ region = 'ADD ') { $ Title = $ _ POST ['title']; $ Content = $ _ POST ['content']; // If you strictly follow MVC, the model should be called here $ Con = mysql_connect ('localhost', 'root', '123 '); If (! $ Con) { Die ('connection failed! '); } Mysql_select_db ('news', $ con ); $ SQL = "insert into question (null, '$ title',' $ content ','')"; If (mysql_query ($ SQL, $ con )) { // 1. Generate static files $ 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 in append mode // 3. Read cyclically // If the last part of the file is not read, read it all the time. While (! Feof ($ fp )) { // Read a row $ Row = fgets ($ fp ); // Replace the placeholder => You can define a complete replacement rule function. $ Row = str_replace ('% title %', $ title, $ row); // if $ row is not assigned again, the $ row value will not change. $ Row = str_replace ('% content %', $ content, $ row ); Fwrite ($ html_fp, $ row); // 4. Write the content to a static file } // 5. The file must be closed Fclose ($ html_fp ); Fclose ($ fp ); Echo "added successfully. <A href = 'newslist. Php'> Click to view the news! </A> "; } Else { Die ('add failed! '); } } // 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 /* <Html> <Head> <Meta charset = "UTF-8"/> <Title> % title % </title> </Head> <Body> <H1> % title % <Pre> % content % </pre> </Body> </Html> */ |
Solution: If a static file exists and is generated within 30 seconds, the static page is directly returned (with time delay)
The code is as follows: |
Copy code |
/* | ------------------ | <Www.111cn.net> | ------------------ */ Header ('content-type: text/html; charset = utf-8 '); $ Id = $ _ GET ['id']? Intval ($ _ GET ['id']): ''; If ($ id = '') die ('Enter the news id 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 here $ 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 ob cache Header ('content-type: text/html; charset = utf-8 '); Echo 'Echo '<table style = "border: 1px solid red;" cellspacing = "0" width = "400px" height = "200px"> '; Echo '<tr> <td> problem details </td> </tr> '; Echo "<tr> <td> title: {$ row ['question _ title']} </td> </tr> "; Echo "<tr> <td> Details: {$ row ['question _ detail ']} </td> </tr> "; Echo '</table> '; $ Ob_str = ob_get_contents (); // 3. Save ob_str to a static file page. Take note of the file name: 1. Unique identification of the News 2. Conducive to seo File_put_contents ("news-id-". $ id. ". html", $ ob_str );
// 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 ); } Else { Echo 'no resource found! '; } |
From the above, we can see that the first method for generating static pages is more suitable for post-page maintenance. The latter is difficult to maintain. We recommend the first method.