PHP static page implementation scheme (Template tag)

Source: Internet
Author: User
The benefits of static pages will not be mentioned. the implementation method we will introduce below is to use the defined template page and tag, and then use php to read the template page and enter the analysis and replacement, here are two examples. solution: Use the template replacement technology (no time delay... the benefits of static pages will not be mentioned. the implementation method we will introduce below is to use the defined template page and tag, and then use php to read the template page and enter the analysis and replacement, here are two examples.

Solution: Use the template replacement technology (no time delay). The code is as follows:

/* | ---------------- |
 
  
| ---------------- */$ Response = $ _ POST ['unknown']; // add operation if ($ response = 'ADD ') {$ title = $ _ POST ['title']; $ content = $ _ POST ['content']; // If you strictly follow MVC, here we should call model $ 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 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 closed fclose ($ html_fp); fclose ($ fp); echo "added successfully. Click to view news! ";} Else {die ('failed to add! ') ;}} In the news list, click the link to view details. you can change it to the generated static page address to directly access the static file. the code is as follows:    
    
  % Title %    % Title %
  
%content%
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:/* | ---------------- | | ---------------- */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 ob cache header ('content-type: text/html; charset = utf-8 '); echo' '; Echo' '; Echo' '; Echo" "; Echo" "; Echo'
Problem Details
Title: {$ row ['question _ title']}
Details: {$ row ['question _ detail ']}
'; $ Ob_str = ob_get_contents (); // 3. save the ob_str file to a static file page. the file name must be: 1. uniquely identifies the News 2. seo file_put_contents ("news-id -". $ id. ". html ", $ ob_str); // closes the database connection (not required; otherwise, the database is automatically closed after the script is executed) mysql_close ($ con );} no resources found in else {echo! ';}

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.

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.