Php generates a complete html static page instance

Source: Internet
Author: User
Tags php database

If you are an seo worker, you may want to convert all the PHP files into html pages. This will benefit the website ranking and reduce the apache load on the server, next I will introduce a php static page generation instance.

Addform. php file

The Code is as follows: Copy code


<Form action = "add. php" method = "post">
News Title:
<Input type = "text" name = "title"/> <br>
News: <br>
<Textarea name = "content" rows = "10" cols = "50">

</Textarea> <br>

<Input type = "submit" name = "submit" value = "submit"/>

</Form>

Add. php file

The Code is as follows: Copy code

<? Php
Require_once ("mysql_inc.php"); // reference conn. php to connect to the database

$ Title = $ _ POST ['title'];
$ Content = $ _ POST ['content']; // obtain the form variable


// Create a text document with the following values automatically counted
$ Countfile = "count.txt ";
If (! File_exists ($ countfile ))
{
Fopen ($ countfile, "w"); // if this file does not exist,
}
$ Fp = fopen ($ countfile, "r ");
$ Num = fgets ($ fp, 20 );
$ Num = $ num + 1; // The value is automatically added each time.
Fclose ($ fp );
$ Fp = fopen ($ countfile, "w ");
Fwrite ($ fp, $ num); // update its value
Fclose ($ fp );


// Obtain the HTML path by using the value automatically counted above $ path
$ Houzui = ". html ";
$ Path = $ num. $ houzui;
// The result is automatic growth, such as 1.html, 2.html, 3.html .......... 1 is automatically added when a news item is added.

// Use the following SQL statement to add data to the table news
$ SQL = "insert into news (id, title, content, path) values ('','". $ title. "','". $ content. "','". $ path. "')";
$ Query = mysql_query ($ SQL );

// Replace the data obtained from the form with the {title} and {content} tags in the template.
$ Fp = fopen ("mode.html", "r"); // read-only open Template
$ Str = fread ($ fp, filesize ("mode.html"); // read the template content
$ Str = str_replace ("{title}", $ title, $ str );
$ Str = str_replace ("{content}", $ content, $ str); // replace content
Fclose ($ fp );

$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, $ str); // write the replaced content into the generated HTML file.
Fclose ($ handle );


// Close the work:
Echo "<a href = $ path target = _ blank> View the news just added </a> ";


Mysql_inc.php database connection file

The Code is as follows: Copy code

<? 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 ("", "& nbsp", $ content ));
Return $ content;
}
}

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

 

 


?>

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.