<html>
<head>
<title>一篇文章</title>
</head>
<body>
<div id = "headline">%headline%</div>
<div id = "content">%content%</div>
</body>
</html>
代码
<?php
//Replace函数用于将从模版文件中读取的内容中的关键字替换成变量中的内容
function Replace($row, $headline = ‘‘, $content = ‘‘)
{
//替换参数中的关键字
$row = str_replace("%headline%", $headline, $row);
$row = str_replace("%content%", $content, $row);
//返回替换后的结果
return $row;
}
//主程序
$connection = mysql_connect("localhost", "username", "password") or die(mysql_error());
$database = mysql_select_db($connection, "dbname") or die(mysql_error());
//新添加的文章信息
$headline = $_POST[‘headline‘];
$content = $_POST[‘content‘];
//生成文件名,这里用日期时间
$filename = ‘S‘.date("YmdHis").‘.html‘;
//执行SQL语句
$sql = "insert into news values(‘$headline‘, ‘$content‘, ‘$filename‘)";
$res = mysql_query($sql);
//根据SQL执行语句返回的bool型变量判断是否插入成功
if($res)
{
//模版文件指针
$f_tem = fopen("template.html","r");
//生成的文件指针
$f_new = fopen($filename,"w");
//循环读取模版文件,每次读取一行
while(!feof($f_tem))
{
$row = fgets($f_tem);
//替换读入内容中的关键字
$row = Replace($row, $headline, $content);
//将替换后的内容写入生成的HTML文件
fwrite($f_new, $row);
}
//关闭文件指针
fclose($f_new);
fclose($f_tem);
//提示
echo "OK!";
}
else
echo "Failed!";
mysql_close();
?>
<img width=‘0‘ height=‘0‘ src=‘counter.php?fileID=S001‘ />