首页 > php教程 > php手册 > 正文

php页面静态化 适用于添加 更新文章内容 模板文件生成html

php中文网
发布: 2016-06-06 20:01:57
原创
1229人浏览过

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

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

1  有利于seo

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

二  两种方式去实现静态化

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

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

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

<!DOCTYPE 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.
-->
<html>
    <head>
        <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('#editor_id');
        });
        </script>
    </head>
    <body>
       <form name="addart" action="addart.php" method="post">
            <ul style="float:left;">
                <li style="width:50px;height:28px;"> 标题</li>
                <li style="height:28px;">作者</li>
                <li style="height:28px;">内容</li>
            </ul>
            <ul>                
                <li><input type="text" name="title" style="width:300px;height:28px;border:solid 1px activeborder;" ></li>
                <li><input type="text"  name="autoher" style="width:250px;height:28px;border:solid 1px activeborder;margin-top:10px;"></li>
                <li><textarea id="editor_id" name="content" style="width:400px;height:300px;margin-top:10px;"></textarea></li>
                <li><input value="重置" type="reset"><input value="提交" type="submit"></li>
            </ul>
        </form>
        <!--<ul>
         <li>最新动态</li>
         <li><a href="html/art_1.html">刘云山对芬兰进行正式访问</a></li>
         <li><a href="html/art_2.html">新疆和田警民联手制服3名歹徒 9个暴恐团伙被破获</a></li>
         <li><a href="html/art_3.html">中国驻菲使馆:在菲遭抢劫并非有意针对中国游客</a></li>
         <li><a href="html/art_4.html">伊拉克极端武装处决大批政府军士兵(图)</a></li>
         <li><a href="html/art_5.html">中国今起实施年内第二次定向降准 力挺三农小微</a></li>
         </ul> -->
    </body>
</html>
登录后复制
2 添加文章处理页面
<?php
header("Content-type:text/html;charset=utf-8");
require 'common/fileGenerate.class.php';
require 'common/connect.class.php';
$title=htmlspecialchars($_POST['title']);
$autoher=htmlspecialchars($_POST['autoher']);
$content=htmlspecialchars($_POST['content']);
$db=new connect();
$sql="insert into artnews(title,content,autoher)VALUES('$title','$content','$autoher')";
$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 'html生成完成';
            }else{
                echo 'html生成失败';
            }
        }
    }
    
}
登录后复制
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

逍遥内容管理系统(Carefree CMS)1.3.0
逍遥内容管理系统(Carefree CMS)1.3.0

系统简介逍遥内容管理系统(CarefreeCMS)是一款功能强大、易于使用的内容管理平台,采用前后端分离架构,支持静态页面生成,适用于个人博客、企业网站、新闻媒体等各类内容发布场景。核心特性1、模板套装系统 - 支持多套模板自由切换,快速定制网站风格2、静态页面生成 - 一键生成纯静态HTML页面,访问速度快,SEO友好3、文章管理 - 支持富文本编辑、草稿保存、文章属性标记、自动提取SEO4、全

逍遥内容管理系统(Carefree CMS)1.3.0 1
查看详情 逍遥内容管理系统(Carefree CMS)1.3.0

立即学习PHP免费学习笔记(深入)”;

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




相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号