php生成靜態(tài)頁面的簡(jiǎn)單示例
發(fā)布新聞,實(shí)現(xiàn)新聞頁面靜態(tài)化,真靜態(tài)
add.php
<html>
<head>添加新聞</head>
<body>
<form method="post" action="doadd.php">
新聞標(biāo)題:<input type="text" name="title" size="100"><br>
新聞內(nèi)容:<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://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="../bootstrap/css/common.css">
<title>{title}</title>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>{title}</h1>
<p>{content}</p>
</div>
</div>
</body>
</html>
- PHP生成HTML靜態(tài)頁面實(shí)例代碼
- 使用PHP實(shí)現(xiàn)生成HTML靜態(tài)頁面
- 比較詳細(xì)PHP生成靜態(tài)頁面教程
- php 生成靜態(tài)頁面的辦法與實(shí)現(xiàn)代碼詳細(xì)版
- 談PHP生成靜態(tài)頁面分析 模板+緩存+寫文件
- 用php的ob_start來生成靜態(tài)頁面的方法分析
- php文章內(nèi)容分頁并生成相應(yīng)的htm靜態(tài)頁面代碼
- 方便實(shí)用的PHP生成靜態(tài)頁面類(非smarty)
- PHP生成靜態(tài)頁面詳解
- php輸出控制函數(shù)和輸出函數(shù)生成靜態(tài)頁面
相關(guān)文章
Zend Framework 2.0事件管理器(The EventManager)入門教程
這篇文章主要介紹了Zend Framework 2.0事件管理器(The EventManager)入門教程,本文包含快速入門例子、EventManager的選項(xiàng)和方法等,需要的朋友可以參考下2014-08-08thinkPHP中_initialize方法實(shí)例分析
這篇文章主要介紹了thinkPHP中_initialize方法,結(jié)合實(shí)例形式分析了子類調(diào)用父類_initialize方法的原理與相關(guān)操作技巧,需要的朋友可以參考下2016-12-12PHP的new static和new self的區(qū)別與使用
這篇文章主要介紹了PHP的new static和new self的區(qū)別與使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Zend Framework教程之Application用法實(shí)例詳解
這篇文章主要介紹了Zend Framework教程之Application用法,詳細(xì)分析了Zend_Application的功能,定義,參數(shù)含義及相關(guān)使用技巧,需要的朋友可以參考下2016-03-036個(gè)常見的 PHP 安全性攻擊實(shí)例和阻止方法
這篇文章主要介紹了6個(gè)常見的 PHP 安全性攻擊實(shí)例和阻止方法,有對(duì)這方面感興趣的小伙伴歡迎大家來閱讀和學(xué)習(xí)2020-12-12