zl程序教程

您现在的位置是:首页 >  后端

当前栏目

PHP生成静态HTML的技术原理

2023-09-14 08:59:47 时间

PHP页面的静态化很有必要,尤其是在CMS系统中,一些内容一旦生成,基本上不会有变化,这时如果用html将页面静态化,无疑会减少服务其解析PHP页面的负担。以下是看书学来的PHP静态化技术,记录之以备不时之需。
无论是利用框架还是简单的脚本,原理基本一致:就是利用PHP进行文件操作,替换html模板中的动态元素。

用Replace函数即php的str_replace函数将模版文件中读取的内容中的关键字替换成变量中的内容,从而实现简单的模板分离。

1.新增与回显

insert.htm文件代码:在标题和内容框中分别输入:“这是标题”和“这是内容”


title 添加一条新记录 /title    meta http-equiv="Content-Type" content="text/html; charset=gb2312"    /head    body      p h1 添加一条新记录 /h1 /p      form name="form1" method="post" action="insert.php"        table width="500" border="0" cellspacing="0" cellpadding="0"          tr            td 标题 /td            td input name="title" type="text" id="title" /td          /tr          tr            td 内容 /td            td textarea name="body" cols="35" rows="5" id="body" /textarea /td          /tr          tr            td colspan="2" input type="submit" name="Submit" value="Submit"            input type="reset" name="Submit2" value="Reset" /td          /tr        /table    /form    /body    /html   
//Replace函数用于将从模版文件中读取的内容中的关键字替换成变量中的内容   function Replace($row, $title=, $body=)       //替换参数中的关键字       $row   = str_replace("%title%", $title, $row);       $row   = str_replace("%body%", $body, $row);       //返回替换后的结果       return $row;   //主程序开始   @mysql_connect("localhost", "root","1981427")               //选择数据库之前需要先连接数据库服务器   or die("数据库服务器连接失败");   @mysql_select_db("test")                    //选择数据库mydb   or die("数据库不存在或不可用");   //将表单中的数据通过$_POST方式获取然后存储在相应的变量中   $title = $_POST[title];   $body = $_POST[body];   //生成文件名   $filename = S.date("YmdHis")..htm;   //执行SQL语句   $query = mysql_query("insert into news values($title, $body, $filename)");   //根据SQL执行语句返回的bool型变量判断是否插入成功   if($query)       //模版文件指针       $f_tem = fopen("template.htm","r");       //生成的文件指针       $f_new = fopen(new.htm,"w");       //$f_new = fopen($filename,"w");       //循环读取模版文件,每次读取一行       while(!feof($f_tem))        {           $row = fgets($f_tem);           $row = Replace($row, $title, $body);            //替换读入内容中的关键字           fwrite($f_new, $row);           //将替换后的内容写入生成的HTML文件       }       //关闭文件指针       fclose($f_new);       fclose($f_tem);       //提示     echo "数据插入成功";   else       echo "数据插入失败";   mysql_close();                                  //关闭与数据库服务器的连接  
//主程序开始   @mysql_connect("localhost", "root","orbit")             //选择数据库之前需要先连接数据库服务器   or die("数据库服务器连接失败");   @mysql_select_db("test")                    //选择数据库mydb   or die("数据库不存在或不可用");   $query = mysql_query("select * from news where title=这是标题");   //根据SQL执行语句返回的bool型变量判断是否插入成功   $row = mysql_fetch_array($query);   html    head    title 修改一条新记录 /title    meta http-equiv="Content-Type" content="text/html; charset=gb2312"    /head    body      p h1 修改一条新记录 /h1 /p      form name="form1" method="post" action="mdf_to_staitc.php"        table width="500" border="0" cellspacing="0" cellpadding="0"          tr            td 标题 /td            td input name="title" type="text" id="title" value=" ?php echo $row[title]? " /td          /tr          tr            td 内容 /td            td textarea name="body" cols="35" rows="5" id="body" ?php echo $row[body]? /textarea /td          /tr          tr          input name="filename" type="hidden" value=" ?php echo $row[filename]? "            td colspan="2" input type="submit" name="Submit" value="Submit"          /tr        /table    /form    /body    /html   
//Replace函数用于将从模版文件中读取的内容中的关键字替换成变量中的内容   function Replace($row, $title=, $body=)       //替换参数中的关键字       $row   = str_replace("%title%", $title, $row);       $row   = str_replace("%body%", $body, $row);       //返回替换后的结果       return $row;   //主程序开始   @mysql_connect("localhost", "root","orbit")             //选择数据库之前需要先连接数据库服务器   or die("数据库服务器连接失败");   @mysql_select_db("test")                    //选择数据库mydb   or die("数据库不存在或不可用");   mysql_query("update news set title=".$_POST[title].",body=".$_POST[body]." where filename=".$_POST[filename]." ");   $query = mysql_query("select * from news where filename=".$_POST[filename]." ");   //根据SQL执行语句返回的bool型变量判断是否插入成功   while($record = mysql_fetch_array($query))       echo " pre ";       print_r($record);       //模版文件指针       $f_tem = fopen("template.htm","r");       //生成的文件指针       $f_new = fopen($record[filename],"w");       //循环读取模版文件,每次读取一行       while(!feof($f_tem))        {           $row = fgets($f_tem);           $row = Replace($row, $record[title], $record[body]);    //替换读入内容中的           fwrite($f_new, $row);           //将替换后的内容写入生成的HTML文件       }       //关闭文件指针       fclose($f_new);       fclose($f_tem);   mysql_close();                                  //关闭与数据库服务器的连接   mysql_close();                                  //关闭与数据库服务器的连接  

3.一般的CMS都会记录内容被浏览的信息,例如浏览次数或者浏览者的IP信息等,静态页面要记录这些信息,可以在模板中加入一个长宽都为0的图片,指向计数脚本。

以记录浏览次数为例:
img width=0 height=0 src=counter.php?fileID=S001 /
这样,计数操作可以放到counter.php中进行,又不会破坏网页的静态性。

?php


strapdown.js HTML 内嵌 Markdown 的静态解决方案 只需在 html 文件中引用strapdown.js,然后在 xmp 标签内书写 Markdown 内容即可。浏览器加载时会自动将 Markdown 渲染为 HTML。再方便不过。