失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > PHP实现页面静态化页面

PHP实现页面静态化页面

时间:2023-07-04 05:43:53

相关推荐

PHP实现页面静态化页面

ob_start 打开输出控制缓冲(要求php开启缓存,在php配置文件php.ini文件中可以设置 output_buffering = on)

ob_get_contents 返回输出缓冲区内容

ob_clean 清空(擦掉)输出缓冲区

ob_get_clean 得到当前缓冲区的内容并删除当前输出缓冲区

php生成文件的函数 file_put_contents('文件路径','文件内容')。(当然php中还有其他写文件的方法,如fwrite)

看demo

db.php 链接数据库类(自己链接数据库)

<?php /*** 数据库连接封装*/class Db {//存储类的实例的静态成员变量private static $_instance;//数据库链接静态变量private static $_connectSource;//连接数据库配置private $_dbConfig = array('host' => '127.0.0.1','user' => 'root','password' => 'root','database' => 'test');private function __construct() {}/*** 实例化*/public static function getInstance() {//判断是否被实例化if(!(self::$_instance instanceof self)) {self::$_instance = new self();}return self::$_instance;}/*** 数据库连接*/public function connect() {if(!self::$_connectSource) {//数据库连接// @ 符号可以取消警告提示self::$_connectSource = @mysql_connect($this->_dbConfig['host'],$this->_dbConfig['user'],$this->_dbConfig['password']);if(!self::$_connectSource) {//抛出异常处理throw new Exception('mysql connect error ');}//选择一款数据库mysql_select_db($this->_dbConfig['database'], self::$_connectSource);//设置字符编码mysql_query("set names UTF8", self::$_connectSource);}//返回资源链接return self::$_connectSource;}}?>

<?php //1、连接数据库,然后从数据库里面获取数据//2、把获取到的数据填充到模板文件里面//3、需要把动态的页面转化为静态页面,生成纯静态化文件if(is_file('index.html') && (time() - filemtime('index.html')) < 10) { //设置缓存失效时间require_once('index.html');} else {require_once('db.php');$connect = Db::getInstance()->connect();$sql = "SELECT * FROM news WHERE `category_id` ORDER BY id DESC LIMIT 5";$result = mysql_query($sql, $connect);$news = array();while($row = mysql_fetch_array($result)) {$news[] = $row;}// echo '<pre>';// print_r($news);// die;ob_start(); //开启缓存区//引入模板文件require_once('./2.php'); //动态文件 singwa.php界面同样进过缓冲区// echo ob_get_contents();// die;file_put_contents('index.html', ob_get_contents());/*if(file_put_contents('index.shtml', ob_get_clean())) {echo 'success';} else {echo 'error';}*/}?>

2.php

<title>新闻中心</title>

数据库文件

/*Navicat Premium Data TransferSource Server : bendiSource Server Type : MySQLSource Server Version : 50553Source Host : localhost:3306Source Schema : testTarget Server Type : MySQLTarget Server Version : 50553File Encoding : 65001Date: 22/10/ 11:10:34*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- ------------------------------ Table structure for news-- ----------------------------DROP TABLE IF EXISTS `news`;CREATE TABLE `news` (`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,`category_id` int(4) NOT NULL,`type` int(4) NOT NULL,`s_title` char(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`title` char(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`image` char(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`description` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,`create_time` int(11) NOT NULL,`update_time` int(11) NOT NULL,PRIMARY KEY (`id`) USING BTREE) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;-- ------------------------------ Records of news-- ----------------------------INSERT INTO `news` VALUES (1, 1, 1, 'SD1111111发', '刚改过', '', '而过摄入高', 1540173889, 1540173889);INSERT INTO `news` VALUES (2, 1, 1, '地方撒', '打发又该', '', '阿达尔', 1540173889, 1540173889);INSERT INTO `news` VALUES (3, 1, 1, '打算干啥', '收费收111111', '', '是否公司认购', 1540173889, 1540173889);INSERT INTO `news` VALUES (4, 1, 1, '大师傅', '阿萨德', '', '打发', 1540173889, 1540173889);SET FOREIGN_KEY_CHECKS = 1;

如果觉得《PHP实现页面静态化页面》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。