index.php
<?php
header('content-type:text/html;charset=utf-8');
// 1. 连接数据库
$link = @mysql_connect('localhost','root','aa') or die('连接失败');
// 2. 选择数据库
mysql_query('use php');
// 3. 设置编码
mysql_query('set names utf8');
// 4. 功能SQL语句
$sql = "select * from news;";
$rs = mysql_query($sql);
// 显示模板 - 头
echo '<h1>新闻管理</h1>';
echo '<a>添加新闻</a>';
echo '<hr />';
echo '编号 标题 查看详情';
echo '<br />';
// 5. 循环从资源中取数据
while($row = mysql_fetch_assoc($rs))
{
echo "{$row['id']} {$row['title']} <a href='./newsinfo.php?id={$row['id']}' target='_blank'>查看详情</a> <br />";
}
newsinfo.php
<?php
header("content-type:text/html;charset=utf-8");
// 接受参数
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
// 判断是否非法操作
if(empty($id))
{
echo '非法操作';
exit;
}
// 缓存文件地址赋给变量
$cache_file = "./newsinfo{$id}.html";
// 判断是否过期
$is_time = @filemtime($cache_file); // 成功,返回Unix时间;错,返回0
echo $is_time;
// 判断当前文件是否存在
if(file_exists($cache_file))
{
if(time() - 30 > $is_time)
{
@unlink($cache_file);
}else {
require $cache_file;
die;
}
}
// 1. 连接数据库
$link = @mysql_connect('localhost','root','aa') or die('连接失败');
// 2. 选择数据库
mysql_query('use php');
// 3. 设置编码
mysql_query('set names utf8');
// 4. 功能SQL语句
$sql = "select * from news where id = $id ;";
$rs = mysql_query($sql);
$info = mysql_fetch_assoc($rs);
ob_start();
echo '<h1 style="color: red">新闻详情页</h1>';
echo "编号:".$info['id']."<br />";
echo "标题:". $info['title']."<br />";
echo "关键词:". $info['keywords']."<br />";
echo "描述:". $info['description']."<br />";
echo "内容:". $info['content'];
// 生成缓存文件
file_put_contents("./newsinfo{$id}.html", ob_get_contents());