PHP生成静态页html
时间:2008-09-05
来源:互联网
<?php
session_start();
require('common.php');
require('lib/BluePage/BluePage.class.php');// 包含分页类
$count = $query->result($query->query(\"SELECT count(*) as count FROM gb_content\"),'count');//总行数
$lines = 10; // 每页行数
$pBP = new BluePage($count, $lines) ;// 实例化分页类
$aPDatas = $pBP->get();//分页返回结果
$limit = $aPDatas['offset'] ;//起始行
$result = $query->query('SELECT * FROM gb_content order by id desc limit ' .$limit. ','.$lines);//查询数据
$gblist = array();
while ($row = $query->fetch_array($result)) {// 取一条数据
$row['username'] = htmlentities($row['username']);
$row['content'] = preg_replace('/(http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\\"\\"\s])*)/i','<a target=\"_blank\" href=\"$1\">$1</a>',htmlentities($row['content']));
$gblist[] = $row;
}
$query->free_result($result);
$query->close();
$strHtml = $pBP->getFull( $aPDatas);//html分页条
$smarty->assign('pagePanel',$strHtml);
$smarty->assign('gblist',$gblist);//给模板赋值
$fp = fopen('index.html','w');
fputs($fp,$smarty->fetch('index.tpl'));
fclose($fp);
$smarty->display('index.tpl');//显示模板内容
?>
上面是结合smarty生成静态页增加代码:
$fp = fopen('index.html','w');
fputs($fp,$smarty->fetch('index.tpl'));
fclose($fp);
fetch是取smarty页面内容的方法
如果不用smarty可以这样
ob_start();
--页面内容代码--
$html = ob_get_contents();
$fp = fopen('index.html','w');
fputs($fp,$html);
fclose($fp);
ob_end_clean();
使用html页最最高效的当该例生成index.html后访问http://coderhome.net/book/php/code/guestbook/时即访问http://coderhome.net/book/php/code/guestbook/index.html而不是http://coderhome.net/book/php/code/guestbook/index.php(当然这必须apache设置正确)所以提高访问速度
这是个简单的例子,所以并不代表最正确优化的方案
作了个简单测试:
访问index.html apache每秒响应290次
访问index.php apache每秒响应19次
可以发现静态页是动态页的15倍
session_start();
require('common.php');
require('lib/BluePage/BluePage.class.php');// 包含分页类
$count = $query->result($query->query(\"SELECT count(*) as count FROM gb_content\"),'count');//总行数
$lines = 10; // 每页行数
$pBP = new BluePage($count, $lines) ;// 实例化分页类
$aPDatas = $pBP->get();//分页返回结果
$limit = $aPDatas['offset'] ;//起始行
$result = $query->query('SELECT * FROM gb_content order by id desc limit ' .$limit. ','.$lines);//查询数据
$gblist = array();
while ($row = $query->fetch_array($result)) {// 取一条数据
$row['username'] = htmlentities($row['username']);
$row['content'] = preg_replace('/(http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\\"\\"\s])*)/i','<a target=\"_blank\" href=\"$1\">$1</a>',htmlentities($row['content']));
$gblist[] = $row;
}
$query->free_result($result);
$query->close();
$strHtml = $pBP->getFull( $aPDatas);//html分页条
$smarty->assign('pagePanel',$strHtml);
$smarty->assign('gblist',$gblist);//给模板赋值
$fp = fopen('index.html','w');
fputs($fp,$smarty->fetch('index.tpl'));
fclose($fp);
$smarty->display('index.tpl');//显示模板内容
?>
上面是结合smarty生成静态页增加代码:
$fp = fopen('index.html','w');
fputs($fp,$smarty->fetch('index.tpl'));
fclose($fp);
fetch是取smarty页面内容的方法
如果不用smarty可以这样
ob_start();
--页面内容代码--
$html = ob_get_contents();
$fp = fopen('index.html','w');
fputs($fp,$html);
fclose($fp);
ob_end_clean();
使用html页最最高效的当该例生成index.html后访问http://coderhome.net/book/php/code/guestbook/时即访问http://coderhome.net/book/php/code/guestbook/index.html而不是http://coderhome.net/book/php/code/guestbook/index.php(当然这必须apache设置正确)所以提高访问速度
这是个简单的例子,所以并不代表最正确优化的方案
作了个简单测试:
访问index.html apache每秒响应290次
访问index.php apache每秒响应19次
可以发现静态页是动态页的15倍
作者: 我爱PHP 发布时间: 2008-09-05
大家学习PHP怎么了,呵呵
作者: qsums 发布时间: 2008-09-06
我是新人,学习了
作者: nyqs 发布时间: 2008-09-06
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28