这是本人根据自己学习php技术页面静态化的过程所写的学习笔记,希望能够对大家有所帮助。
1、基本思路:
(1)用户第一次访问这个页面时从数据库读出内容,放在缓存中;
(2)将缓存中的数据写成一个html静态页面的文件;
(3)用户以后访问都去读取生成html文件,然后输出在屏幕上;
(4)每隔一定的时间重新生成一次html静态页面。
2、创建数据库
create table news(id int unsigned primary key auto_increment,title varchar(128) not null,content varchar(256) not null,filename varchar(32)) engine=myisam
3、测试数据
insert into `static_pages_news`.`news` (`id`, `title`, `content`, `filename`) values (null, 'hello1', '北京你好', null), (null, 'hello2', '四川你好', null);
4、代码
news_list.php(新闻列表页面)
sqlhelper.class.php $conn = mysql_connect(localhost, root, root); if (!$conn) { die(连接失败); } mysql_select_db(static_pages_news, $conn); mysql_query(set names utf8); $sql = select * from news; $res = mysql_query($sql); header(content-type:text/html;charset=utf-8); echo 新闻列表; echo 添加新闻; echo
; echo id 标题
; while ($row = mysql_fetch_assoc($res)) { echo {$row['id']} {$row['title']}
; } echo
; mysql_free_result($res); mysql_close($conn);?>
show_news.php(新闻详情页面)
5、不足之处
(1)在查看新闻详情的时候,仍然是一个php页面
(2)实时性不够好,有30s的延时
