搜尋
首頁php教程php手册php页面静态化 适用于添加 更新文章内容 模板文件生成html

一 页面静态化有利有弊 合理的使用php生成html完成网站 静态化设计 1 有利于seo 2 有利于对于一些不经常更新的内容 提高访问效率 二 两种方式去实现静态化 1. 使用文件函数得到静态页面的模板字符串,然后用str_replace函数将需要替换的东西替换了再写入到

一  页面静态化有利有弊 合理的使用php生成html完成网站 静态化设计

1  有利于seo

2  有利于对于一些不经常更新的内容 提高访问效率

二  两种方式去实现静态化

1. 使用文件函数得到静态页面的模板字符串,然后用str_replace函数将需要替换的东西替换了再写入到新的文件中。
2. 利用PHP的输出控制函数(Output Control)得到静态页面字符串,再写入到新的文件中。

本文只讲第一种方式 后续会更新第二种

1 简单的html页面用于添加文章

<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

    
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            a:link{
                color:rgb(10,10,10);
                text-decoration: none;
            }
           form ul li{
                list-style: none;
               }
        </style>
        <script charset="utf-8" src="mykindeditor/kindeditor.js"></script>
        <script charset="utf-8" src="mykindeditor/lang/zh_CN.js"></script>
        <script>
        KindEditor.ready(function(K) {
                window.editor = K.create(&#39;#editor_id&#39;);
        });
        </script>
    
    
       
  • 标题
  • 作者
  • 内容
2 添加文章处理页面
<?php header("Content-type:text/html;charset=utf-8");
require &#39;common/fileGenerate.class.php&#39;;
require &#39;common/connect.class.php&#39;;
$title=htmlspecialchars($_POST[&#39;title&#39;]);
$autoher=htmlspecialchars($_POST[&#39;autoher&#39;]);
$content=htmlspecialchars($_POST[&#39;content&#39;]);
$db=new connect();
$sql="insert into artnews(title,content,autoher)VALUES(&#39;$title&#39;,&#39;$content&#39;,&#39;$autoher&#39;)";
$insert_id=$db->query($sql);
$filename=date('Ymdhis')."_".$insert_id.".html";
$fileGenerate=new fileGenerate();
$fileGenerate->htmlfile($filename,$title,$autoher,$content);
?>
3 生成文件类
<?php /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of fileGenerate
 *
 * @author Administrator
 */
class fileGenerate {
    //html文件按照模板生成函数
    public function htmlfile($filename,$title,$autoher,$content){ 
        //判断静态文件是否存在不存在直接生成 存在删除重新生成
        if(file_exists($filename)){
            unlink($filename);
        }else {            
            $filemodel="art.html";                        //#模板地址
            $file=fopen($filemodel,"rb");                 //#打开模板,得到文件指针
            $temp=fread($file,filesize($filemodel));      //#得到模板文件html代码
            //替换摸版中的内容
            $temp=str_replace("[title]",$title,$temp);
            $temp=str_replace("[autoher]",$autoher,$temp);
            $temp=str_replace("[content]",$content,$temp);
            //生成html文件            
            fwrite(fopen("html/"."$filename","wb"),$temp); #$filename是静态页面的文件名
            if(file_exists("html/"."$filename")){
                echo &#39;html生成完成&#39;;
            }else{
                echo &#39;html生成失败&#39;;
            }
        }
    }
    
}
4 数据库连接
<?php /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Description of connect
 *
 * @author Administrator
 */
class connect {
    //数据库连接
    private $host;
    private $user;
    private $pwd;
    private $dbname;
    private $port;
    private $charset;
    public function __construct(){
        $this->host='localhost';
        $this->user='root';
        $this->pwd='';
        $this->dbname='phptest';
        $this->charset='set names utf8';
        $this->getConnect();
    }
    public function getConnect(){
        $con=@mysql_connect($this->host,$this->user,$this->pwd);
        mysql_select_db($this->dbname)or die("not found.$this->dbname");
        mysql_query($this->charset);
    }
    function query($sql){
        mysql_query($sql);
        return $insert_id=mysql_insert_id();
    }
}
下面给出 源码 数据库sql文件 的下载地址

http://pan.baidu.com/s/1gdIJAPd

有什么不足或是建议 也请大家评论留言




陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中