Php page static principle and tutorial

Source: Internet
Author: User
I mentioned the url pseudo-static problem in the previous section. this is just to create seo-friendly URLs. static html files are generated in real time, which has obvious advantages, static files can speed up access and reduce server pressure. However, when you have a large number of articles

I mentioned the url pseudo-static problem in the previous section. this is just to create seo-friendly URLs. static html files are generated in real time, which has obvious advantages, static files can speed up access and reduce server pressure. However, when you have a large number of articles, they also occupy the disk. the implementation principle is actually very simple, is to use php file operations to generate static html files. Of course, you can also use the php ob operation. I only use the php file operation to generate a static page immediately when publishing an article or updating an article.

Php page static principle:

1. create a test database and a user table as follows (several test databases are inserted by yourself ):


CREATE TABLE IF NOT EXISTS `news` (  `id` int(10) NOT NULL AUTO_INCREMENT,  `title` varchar(128) DEFAULT NULL,  `content` text,  `time` int(10) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;

2. create the connection data file conn. php


 GetMessage () ;}?>

3. display the news list (news. php). Note that the connection is a static html connection, which is not generated yet. of course, the link cannot be opened:


 Add article
 Query ($ SQL) as $ row) {echo "{$ row ['title']} ---- modify an article
";}?>

The page effect is as follows:

4. add and modify the article page:


 
 Query ($ SQL)-> fetch () ;}?>

5.template.html


{Title}  
       {Title} posted on {time} {content}

6. action. php is of course used to generate and update static files:


 Query ($ SQL); $ id = $ dbh-> lastInsertId (); $ filename = "news_registry.id=.html"; $ fp_tmp = fopen ("template.html", "r "); $ fp_html = fopen ($ filename, "w"); while (! Feof ($ fp_tmp) {$ row = fgets ($ fp_tmp); $ row = replace ($ row, $ title, $ content, date ('Y-m-d H: i: S', $ time); fwrite ($ fp_html, $ row);} fclose ($ fp_tmp); fclose ($ fp_html ); echo "added successfully and generated static files";} else {$ SQL = "update news set title = $ title, content = $ content, time = $ time where id = {$ _ POST ['id']} "; $ dbh-> query ($ SQL); $ filename =" news_000000000000_post0000'id'00000000.html "; @ unlink ($ filename); $ fp_tmp = fopen ("template.html", "r"); $ fp_htm L = fopen ($ filename, "w"); while (! Feof ($ fp_tmp) {$ row = fgets ($ fp_tmp); $ row = replace ($ row, $ title, $ content, date ('Y-m-d H: i: S', $ time); fwrite ($ fp_html, $ row);} fclose ($ fp_tmp); fclose ($ fp_html ); echo "updated successfully and updated static files";} // replace function replace ($ row, $ title, $ content, $ time) row by row) {$ row = str_replace ("{title}", $ title, $ row); $ row = str_replace ("{content}", $ content, $ row ); $ row = str_replace ("{time}", $ time, $ row); return $ row;}?>

Everything is okay. of course, this is a simple implementation of php page static principle!

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.