您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP页面静态化:真静态的两种方案,php静态_PHP教程

2025/11/6 0:42:57发布18次查看
php页面静态化:真静态的两种方案,php静态----------------------------------------------------------------------------------------------
方案1:如果静态文件存在,且生成时间30秒内,直接返回静态页面(有时间延迟)
/*
|------------------
|
|------------------
*/
header('content-type:text/html;charset=utf-8');$id = $_get['id'] ? intval($_get['id']) : '';if($id === '') die('请输入要查询的新闻id!');$html_file = news-id-.$id..html;//1.主要代码if(file_exists($html_file) && filemtime($html_file) + 30 >= time()){ echo '静态页面:'; echo file_get_contents($html_file);exit;}//这里也可以使用db工具类$con = mysql_connect('localhost', 'root', '123456');if(!$con){ die('连接失败!');}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.启动ob缓存 header('content-type:text/html;charset=utf-8'); echo ''; echo '; echo '问题详细内容
'; echo 标题:{$row['question_title']}
; echo 详细:{$row['question_detail']}
; echo '
'; $ob_str = ob_get_contents(); //3.把ob_str保存到一个静态文件页面,取文件名有讲究:1.唯一标识该新闻 2.利于seo file_put_contents(news-id-.$id..html, $ob_str);
//关闭数据库连接(非必须; 非长连接下,脚本执行完会自动关闭)
mysql_close($con);}else{ echo '没有查询到资源!';}
@黑眼诗人

方案2:使用模板替换技术(没有时间延迟)
/*
|------------------
|
|------------------
*/
$oper = $_post['oper'];//添加操作if($oper === 'add'){ $title = $_post['title']; $content = $_post['content']; //如果严格按mvc,这里应该调用model了 $con = mysql_connect('localhost', 'root', '123456'); if(!$con) { die('连接失败!'); } mysql_select_db('news', $con); $sql = insert into question(null, '$title', '$content', ''); if(mysql_query($sql, $con)) { //1.生成静态文件 $id = mysql_insert_id(); $html_filename = 'news-id'.$id.'.html'; $html_fp = fopen($html_filename, 'w'); //2.把模板文件读取(news.html) $fp = fopen('news.tpl', 'r'); //r 只读方式打开; r+ 读写方式打开; w 写入方式打开:文件内容将被清空!如果文件不存在将创建; a 以追加的方式打开 //3.循环读取 //如果没有读到文件的最后,就一直读取 while(!feof($fp)) { //一行行读 $row = fgets($fp); //把占位符替换掉 => 可以自定义完整的替换规则函数 $row = str_replace('%title%', $title, $row);//如果不重新赋值$row, $row值不会改变 $row = str_replace('%content%', $content, $row); fwrite($html_fp, $row);//4.将内容写入静态文件 }
//5.文件必须关闭 fclose($html_fp); fclose($fp); echo 添加成功。点击查看新闻!; } else { die('添加失败!'); }}//此时在新闻列表内,点击的链接,可以改成生成的静态页面地址,直接进入静态文件。//news.tpl模板文件/* %title% %title% %content%
*/---------------------------------------------------------------------------------------------
php静态化页面方法,伪静态的也可以
这里是jetee.cn中静态化会员列表部分,根据这部分代码,可以理解静态化的一些概念。
静态化函数部分
/**
* @get list member item 获得静态化模板中要替换变量的内容。
* @param str: to replace the string
* @return string
*/
function get_staticize_replace_str()
{
$replace_str=; //replace string
$query=select member_id,email_name from member;
$result=mysql::query($query);
while($row=mysql::fetch_assoc($result))
{
$replace_str.=
.$row[member_id].;
$replace_str.=.$row[email_name].
;
}
return $replace_str;
}
/**
* @替换静态化模板中的变量生成静态化页面。
* @
* @return void
*/
function staticize_list_member()
{
$replace_str=$this->get_staticize_replace_str();
$templet=fopen(tendao_dir./templets/default/list_member.html,r);
$new_file=fopen(tendao_dir./member/list_member.html,w);
while(!feof($templet))
{
$ripe=fgets($templet);
$ripe=str_replace({member_items},$replace_str,$ripe);
fwrite($new_file,$ripe);
}
if (file_exists(tendao_dir./member/list_member.html)) {
msg(静态化会员列表成功!返回主页中……,tendao_root,0,3000);
exit();
} else {
msg(静态化会员列表没有成功!返回主页中……,tendao_root,0,3000);
}
fclose($templet);
fclose($new_file);
}
/**
* @静态化模板
* @
......余下全文>>
 php对于页面静态化问题
方法很多啊
在php中 用include_once 就可以了
只是包含文件的时候注意 你分离出去的文件只有
中的部分
再就是看好页面样式
好的程序员 会将很多部分分离
这样便于统一管理
http://www.bkjia.com/phpjc/851760.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/851760.htmltecharticlephp页面静态化:真静态的两种方案,php静态 ---------------------------------------------------------------------------------------------- 方案1:如果静态文件存...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product