PHP Generate HTML Static page full Instance _php tutorial

Source: Internet
Author: User
Tags php database
If you are an SEO worker you estimate that the PHP files are all converted to HTML page, this can be good for the ranking of the site, but also reduce the server Apache load, I would like to introduce a PHP generation static page instance.

addform.php file

The code is as follows Copy Code


add.php file

The code is as follows Copy Code

Require_once ("mysql_inc.php"); referencing conn.php, connecting to a database

$title =$_post[' title ';
$content =$_post[' content '; Get form variables


The following article establishes a document, whose value is automatically counted
$countfile = "Count.txt";
if (!file_exists ($countfile))
{
fopen ($countfile, "w"); If this file does not exist, it automatically establishes a
}
$FP =fopen ($countfile, "R");
$num =fgets ($FP, 20);
$num = $num +1; Each time its value is automatically added to a
Fclose ($FP);
$FP =fopen ($countfile, "w");
Fwrite ($fp, $num); Update its value
Fclose ($FP);


Get the path to the HTML using the above automatically counted value $path
$houzui = ". html";
$path = $num. $houzui;
The resulting path is automatically growing, such as 1.html,2.html,3.html .... Add a piece of news and automatically 1

The following SQL statement is used to add data to table news
$sql = "INSERT INTO News (Id,title,content,path) VALUES (', '". $title. "', '". $content. "', '". $path. "')";
$query =mysql_query ($sql);

The following is the key to replacing the data obtained from the form with the {title},{content} tag in the template
$FP =fopen ("mode.html", "R"); Read-only open template
$str =fread ($fp, FileSize ("mode.html"));//Read the contents of the template
$str =str_replace ("{title}", $title, $STR);
$str =str_replace ("{content}", $content, $STR);//replace content
Fclose ($FP);

$handle =fopen ($path, "w"); Write to open News path
Fwrite ($handle, $STR); Write the content you just replaced into the generated HTML file
Fclose ($handle);


Finishing work:
echo "View the news you just added";


mysql_inc.php Database Connection File

The code is as follows Copy Code

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.html www.bkjia.com true http://www.bkjia.com/PHPjc/632767.html techarticle If you are an SEO worker you estimate that the PHP files are all converted to HTML page, this can be good for the site ranking, but also to reduce the server Apache load, below I ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.