search
Homephp教程php手册PHP页面静态化学习笔记之四:简易新闻系统v1.0

这是本人根据自己学习PHP技术页面静态化的过程所写的学习笔记,希望能够对大家有所帮助。 1、基本思路: (1)用户第一次访问这个页面时从数据库读出内容,放在缓存中; (2)将缓存中的数据写成一个html静态页面的文件; (3)用户以后访问都去读取生成html

这是本人根据自己学习PHP技术页面静态化的过程所写的学习笔记,希望能够对大家有所帮助。


1、基本思路:

(1)用户第一次访问这个页面时从数据库读出内容,放在缓存中;

(2)将缓存中的数据写成一个html静态页面的文件;

(3)用户以后访问都去读取生成html文件,然后输出在屏幕上;

(4)每隔一定的时间重新生成一次html静态页面。

2、创建数据库

create table news(
id int unsigned primary key auto_increment,
title varchar(128) not null,
content varchar(256) not null,
filename varchar(32)) engine=MyISAM

3、测试数据

INSERT INTO `static_pages_news`.`news` (`id`, `title`, `content`, `filename`) VALUES (NULL, 'hello1', '北京你好', NULL), (NULL, 'hello2', '四川你好', NULL);

4、代码

news_list.php(新闻列表页面)
<?php //新闻列表
    //查询数据库,获取信息=>SqlHelper.class.php

    $conn = mysql_connect("localhost", "root", "root");
    if (!$conn) {
        die("连接失败");
    }
    mysql_select_db("static_pages_news", $conn);
    mysql_query("set names utf8");
    
    $sql = "select * from news";
    $res = mysql_query($sql);
    
	header("content-type:text/html;charset=utf-8");
    echo "<h1 id="新闻列表">新闻列表</h1>";
    echo "<a href="#">添加新闻</a><hr>";
    echo "
"; echo ""; while ($row = mysql_fetch_assoc($res)) { echo ""; } echo "
id 标题 查看详情
{$row['id']} {$row['title']} 查看详情
"; mysql_free_result($res); mysql_close($conn); ?>
show_news.php(新闻详情页面)
<?php $id = @$_GET['id'];
//构建文件名
$html_filename = "new_id" . $id . ".html";
$html_path = dirname(__FILE__) . "/" . $html_filename;
//判断html页面是否有并且页面最后修改时间到现在小于30s
if (file_exists($html_path) && filemtime($html_path) &#43; 30 < time()) {
    //直接访问html页面(把html页面的内容echo浏览器)
    echo file_get_contents($html_path);
    exit ;
}

$conn = mysql_connect("localhost", "root", "root");
if (!$conn) {
    die("连接失败");
}
mysql_select_db("static_pages_news", $conn);
mysql_query("set names utf8");

$sql = "select * from news where id=$id";
$res = mysql_query($sql);
//开启缓存
ob_start();
if ($row = mysql_fetch_assoc($res)) {
    header("content-type:text/html;charset=utf-8");
    echo "<table border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
    echo "<tr><td>新闻详细内容</td></tr>";
    echo "<tr><td>{$row['title']}</td></tr>";
    echo "<tr><td>{$row['content']}</td></tr>";
    echo "";
} else {
    echo "没有结果";
}

$html = ob_get_contents();
$my_header = "<meta http-equiv="Content-Type" content="text/html; charset=utf-8">";
//把ob写进html文档
file_put_contents($html_path, $my_header . $html);
mysql_free_result($res);
mysql_close($conn);
?>

5、不足之处

(1)在查看新闻详情的时候,仍然是一个PHP页面

(2)实时性不够好,有30s的延时


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)