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('#editor_id'); }); </script>
<?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); ?>
<?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生成失败'; } } } }
<?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(); } }
http://pan.baidu.com/s/1gdIJAPd
有什么不足或是建议 也请大家评论留言

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

CakePHP 是 PHP 的開源框架。它旨在使應用程式的開發、部署和維護變得更加容易。 CakePHP 基於類似 MVC 的架構,功能強大且易於掌握。模型、視圖和控制器 gu

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

CakePHP 是一個開源MVC 框架。它使應用程式的開發、部署和維護變得更加容易。 CakePHP 有許多函式庫可以減少大多數常見任務的過載。

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲
