php生成静态页面的简单示例

时间:2021-05-26

发布新闻,实现新闻页面静态化,真静态


add.php
复制代码 代码如下:
<html>
<head>添加新闻</head>

<body>
<form method="post" action="doadd.php">
新闻标题:<input type="text" name="title" size="100"><br>
新闻内容:<textarea name="content" cols="100" rows="25"></textarea><br>
<input type="submit" name="提交">
</form>
</body>
</html>

config.php
复制代码 代码如下:
<?php
define("HOST", "localhost");
define("USER", "justfan");
define("PWD", "justfan");
define("DB", "justfanDB");
define("PORT", "3360");
?>

DB_class.php
复制代码 代码如下:
<?php
class DB
{
private $host = '';
private $uname = '';
private $pwd = '';
private $port = '';
private $db = '';
public static $instance = null;

private function __construct($host , $uname , $pwd , $port , $db)
{
$this->host = $host;
$this->uname = $uname;
$this->port = $port;
$this->pwd = $pwd;
$this->db = $db;

mysql_connect($host,$uname,$pwd);
mysql_select_db($this->db);
}

public static function Instance()
{
if(Db::$instance==null){
include 'config.php';
return Db::$instance = new DB(HOST, USER, PWD, PORT, DB);
}
else
return Db::$instance;
}

public function query($sql)
{
mysql_query("SET NAMES UTF8");
$query = mysql_query($sql) or die($sql." error");
if(!$query) return false;
else return $query;
}


public function getAll($sql)
{
$query = $this->query($sql);
if($query)
{
while($ret = mysql_fetch_assoc($query))
{
$result[] = $ret;
}
}
return $result;
}


}
?>

doadd.php
复制代码 代码如下:
<?php
include 'DB_class.php';
$db = DB::Instance();

$title=$_POST["title"];
$content=$_POST["content"];

$num = uniqid();
$houzui=".html";
$filename=date('Ymd').'/'.$num.$houzui;

$sql="insert into news(title,content,path) values ('{$title}' , '{$content}' , '{$filename}')";
$query = $db->query($sql);

$fp=fopen("model.htm","r");
$str=fread($fp,filesize("model.htm"));
$str=str_replace("{title}",$title,$str);
$str=str_replace("{content}",$content,$str);
fclose($fp);

$dir = dirname($filename);
if(!is_dir($dir)){
mkdir($dir);
}

$handle=fopen($filename,"w");
fwrite($handle,$str);
fclose($handle);

echo "<a href={$filename} target=_blank>查看刚才添加的新闻</a>";
echo "<a href='add.php'>添加新闻</a>";
?>

model.htm
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://mon.css">
<title>{title}</title>
</head>
<body>

<div class="container">
<div class="jumbotron">
<h1>{title}</h1>
<p>{content}</p>
</div>
</div>
</body>
</html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章