php產生html靜態頁面完整執行個體_PHP教程

來源:互聯網
上載者:User
如果你是一個seo工作者你估計要求把php檔案全部轉換成html頁面了,這樣可以對網站排名有好處,同時也可以減輕伺服器apache負載了,下面我來介紹一個php產生靜態頁面執行個體。

addform.php檔案

代碼如下 複製代碼


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 "查看剛才添加的新聞";


mysql_inc.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.'
'.$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","
",str_replace(" "," ",$content));
return $content;
}
}

$db=new mysql("localhost","root","","database","utf8");


?>

http://www.bkjia.com/PHPjc/632767.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/632767.htmlTechArticle如果你是一個seo工作者你估計要求把php檔案全部轉換成html頁面了,這樣可以對網站排名有好處,同時也可以減輕伺服器apache負載了,下面我...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.