4.添加修改文章页面:
//获取修改的内容 if($_GET['id']){
require_once \"conn.php\";
$sql = \"select * from news where id={$_GET['id']}\"; $res = $dbh->query($sql)->fetch(); } ?>
5.用于生成静态文件的页面模板template.html
{title} 6.action.php当然是用来生成和更新静态文件的:
//表单处理操作
header(\"content-type:text/html;charset=utf-8\"); require_once 'conn.php'; $title = $_POST['title'];
$content = $_POST['content']; $time = time();
if($_POST['submit']=='添加'){ $sql = \"insert into news values('','$title','$content',$time)\"; $dbh->query($sql);
$id = $dbh->lastInsertId(); $filename = \"news_{$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
{title}发表于{time}
{content}
H:i:s',$time));
fwrite($fp_html,$row); }
fclose($fp_tmp); fclose($fp_html);
echo \"添加成功并生成静态文件\"; }else{ $sql = \"update news set title = $title , content = $content ,time = $time where id ={$_POST['id']}\";
$dbh->query($sql);
$filename = \"news_{$_POST['id']}.html\"; @unlink($filename);
$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));
一切ok啦,当然这是简单的实现php页面静态化原理!
欢迎转载! 原文地址: http://www.phpddt.com/php/php-rewrite.html ,转载请注明地址,谢谢!
fwrite($fp_html,$row); }
fclose($fp_tmp); fclose($fp_html);
echo \"更新成功并更新静态文件\"; }
//逐行替换函数
function replace($row,$title,$content,$time){ $row=str_replace(\"{title}\",$title,$row);
$row=str_replace(\"{content}\",$content,$row); $row=str_replace(\"{time}\",$time,$row); return $row; } ?>