首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

php页面静态化原理及教程

2020-09-04 来源:华拓网
上一讲说了url 伪静态的问题,这只是创建了对seo友好的url,而真静态会生成静态的html文件,很明显优势更大,静态文件能够加快访问速度,减少服务器压力等等,但是,当你的文章数量很多事,也是很占磁盘的,实现原理其实很简单,就是利用php的文件操作生成静态的html文件。当然也可以利用php的ob操作完成,我这里为了达到当发布文章或者更新文章立即生成静态页面效果只用了一般php文件操作。 php页面静态化原理:(由php点点通提供php教程)

1.创建测试数据库test,建立user表如下(自己插入几条测试数据库):

     

2.建立连接数据文件conn.php

         

3.显示新闻列表(news.php),注意,其连接为静态html连接,这时还没生成,当然链接打不开:

$dsn = \"mysql:host=localhost;dbname=test;\"; $user = \"root\"; $password = \"\"; try{

$dbh = new PDO($dsn,$user,$password); }catch(PDOException $e){

echo \"连接失败\".$e->getMessage(); } ?>

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 ;



 添加文章 


  require_once \"conn.php\";

 $sql = \"select * from news\";

 foreach($dbh->query($sql) as $row){  echo

href='news_{$row['id']}.html'>{$row['title']}----修改文章
\";  }  ?>

页面效果如下:

\"4.添加修改文章页面:



  //获取修改的内容  if($_GET['id']){

 require_once \"conn.php\";

 $sql = \"select * from news where id={$_GET['id']}\";  $res = $dbh->query($sql)->fetch();  }  ?>



 标题:\"/>

 内容:

col=40

 \" />  \" /> 

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; } ?>

因篇幅问题不能全部显示,请点此查看更多更全内容