[PHP] PHP static page: two static methods

Source: Internet
Author: User
Optional * | ---------------- | solution 1: If a static file exists and is generated within 30 seconds, the static page is directly returned (with time delay) www.farwish.com | ---------------- * header

Response/* | ---------------- | solution 1: If a static file exists and is generated within 30 seconds, the static page (with time delay) is directly returned www.farwish.com | ---------------- */header

Bytes ----------------------------------------------------------------------------------------------

/*
| ------------------
| Solution 1: If a static file exists and is generated within 30 seconds, the static page is directly returned (with time delay)
| ------------------
*/
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 );
  
// 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! ';}


/*
| ----------------------------------------------------
| Solution 2: Use the template Replacement Technology (no time delay)
| ----------------------------------------------------
*/
$ 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. Click to view news! ";} 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 /* % Title % % Title %
%content%
*/

Link: http://www.cnblogs.com/farwish/p/3874264.html

@ Blackeye poet

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.