如果你是一個seo工作者你估計要求把php文件全部轉換成html頁面了,這樣可以對網站排名有好處,同時也可以減輕服務器apache負載了,下面我來介紹一個php生成靜態頁面實例.
addform.php文件代碼如下:
- <form action="add.php" method="post" >
- 新聞標題:
- <input type="text" name="title" /><br>
- 新聞內容:<br>
- <textarea name="content" rows="10" cols="50" >
- </textarea><br>
- <input type="submit" name="submit" value="提交"/>
- </form>
add.php文件代碼如下:
- require_once("mysql_inc.php"); //引用conn.php,連接數據庫
- $title=$_POST['title'];
- $content=$_POST['content']; //獲得表單變量
- //以下建立一文本文檔,其值自動計數
- $countfile="count.txt";
- if(!file_exists($countfile))
- {
- fopen($countfile,"w"); //如果此文件不存在,則自動建立一個
- }
- $fp=fopen($countfile,"r");
- $num=fgets($fp,20);
- $num=$num+1; //每次其值自動加一
- fclose($fp);
- $fp=fopen($countfile,"w");
- fwrite($fp,$num); //更新其值
- fclose($fp);
- //利用上面自動計數的值獲得HTML的路徑$path
- $houzui=".html";
- $path=$num.$houzui;
- //這樣形成的路徑是自動增長的,如1.html,2.html,3.html……….添加一條新聞便自動加上1
- //以下用SQL語句添加數據至表 news
- $sql="insert into news (id,title,content,path) values ('','".$title."','".$content."','".$path."')";
- $query=mysql_query($sql);
- //以下為關鍵之處,把從表單獲得的數據替換模板中的{title},{content}標記
- $fp=fopen("mode.html","r"); //只讀打開模板
- $str=fread($fp,filesize("mode.html"));//讀取模板中內容
- $str=str_replace("{title}",$title,$str);
- $str=str_replace("{content}",$content,$str);//替換內容
- fclose($fp);
- $handle=fopen($path,"w"); //寫入方式打開新聞路徑
- fwrite($handle,$str); //把剛才替換的內容寫進生成的HTML文件
- fclose($handle);
- //收尾工作:
- echo "<a href=$path target=_blank>查看剛才添加的新聞</a>";
mysql_inc.php數據庫連接文件,代碼如下:
- <?php
- class mysql{
- private $host;//
- private $name;//
- private $pass;//
- private $database;//
- private $ut;//
- function __construct($host,$name,$pass,$database,$ut){
- $this->host=$host;
- $this->name=$name;
- $this->pass=$pass;
- $this->database=$database;
- $this->ut=$ut;
- $this->connect();
- }
- function connect(){
- $link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());
- mysql_select_db($this->database,$link) or die("沒發現數據庫".$this->database);
- mysql_query("SET NAMES '$this->ut'");
- }
- function query($sql, $type = '') {
- if(!($query = mysql_query($sql))) $this->show('Say:', $sql);
- return $query;
- }
- function show($message = '', $sql = '') {
- if(!$sql) echo $message;
- else echo $message.'<br>'.$sql;
- }
- function affected_rows() {
- return mysql_affected_rows();
- }
- function result($query, $row) {
- return mysql_result($query, $row);
- }
- function num_rows($query) {
- return @mysql_num_rows($query);
- }
- function num_fields($query) {
- return mysql_num_fields($query);
- }
- function free_result($query) {
- return mysql_free_result($query);
- }
- function insert_id() {
- return mysql_insert_id();
- }
- function fetch_row($query) {
- return mysql_fetch_row($query);
- }
- function version() {
- return mysql_get_server_info();
- }
- function close() {
- return mysql_close();
- }
- function htmtocode($content){
- $content=str_replace("n","<br>",str_replace(" "," ",$content));
- return $content;
- }
- }
- $db=new mysql("localhost","root","","database","utf8");
- ?>
新聞熱點
疑難解答