Generating static pages is a good choice for php to reduce server load and seo website optimization. Therefore, the php static page generation function is a knowledge point that almost all php programmers must understand and master, next, I will introduce you to the Analysis of the Principle of generating static pages in php. If you need to know more, please refer.
Generating html principles
We will write the tag to be generated into a template file, and then use php to read and replace the specified tag with the content we want to replace. Now the mainstream dedecms system does the same.
Generate static page code.
The template does not fill in the html file. For example:
The Code is as follows: |
Copy code |
Temp.html <HTML> <TITLE> {title} </TITLE> <BODY> This is a {file} fileArray; s templets </BODY> </HTML> Templetest. php <? Php $ Title = "tomai international test template "; $ File = "TwoMax Inter test templet, <br> author: Matrix @ Two_Max "; $ Fp = fopen ("temp.html", "r "); $ Content = fread ($ fp, filesize ("temp.html ")); $ Content. = str_replace ("{file}", $ file, $ content ); $ Content. = str_replace ("{title}", $ title, $ content ); Echo $ content; ?> |
This is an ultra-simple php function to generate static pages, but this is not practical in the application. Next I will introduce a database to generate an instance.
1. Create a test database and a user table as follows (several test databases are inserted by yourself ):
The Code is as follows: |
Copy code |
Create table if not exists 'News '( 'Id' int (10) not null AUTO_INCREMENT, 'Title' varchar (128) default null, 'Content' text, 'Time' int (10) default null, Primary key ('id ') ) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 12;
|
2. Create the connection data file conn. php
The Code is as follows: |
Copy code |
<? Php $ Dsn = "mysql: host = localhost; dbname = test ;"; $ User = "root "; $ Password = ""; Try { $ Dbh = new PDO ($ dsn, $ user, $ password ); } Catch (PDOException $ e ){ Echo "connection failed". $ e-> getMessage (); } ?> |
3. display the news list (news. php). Note that the connection is a static html connection, which is not generated yet. Of course, the link cannot be opened:
The Code is as follows: |
Copy code |
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/> <A href = "add. php"> add an article </a> <Hr> <? Php Require_once "conn. php "; $ SQL = "select * from news "; Foreach ($ dbh-> query ($ SQL) as $ row ){ Echo "<a href0000'news_00000000row0000'id'000000000000.html '>{$ row ['title']} </a> ---- <a href = 'add. php? Id = {$ row ['id']} '> modify an article </a> <br> "; } ?> |
4. Add and modify the article page:
The Code is as follows: |
Copy code |
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/> <? Php // Obtain the modified content If ($ _ GET ['id']) { Require_once "conn. php "; $ SQL = "select * from news where id = {$ _ GET ['id']}"; $ Res = $ dbh-> query ($ SQL)-> fetch (); } ?> <Form action = "action. php" method = "post"> Title: <input type = "text" name = "title" value = "<? = @ $ Res ['title']?> "/> <Br/> Content: <textarea name = "content" col = 40 row = 4> <? = @ $ Res ['content']?> </Textarea> <br/> <Input type = "hidden" name = "id" value = "<? = $ _ GET ['id']?> "/> <Input type = "submit" name = "submit" value = "<? Php echo $ _ GET ['id']? 'Modify': 'add'?> "/> </Form> |
5.template.html
The Code is as follows: |
Copy code |
<Html> <Head> <Title> {title} </title> <Meta http-equiv = "content-type" content = "text/html; charset = UTF-8"/> </Head> <Body> {Title} posted on {time} <Hr> {Content} </Body> </Html>
|
6. action. php is of course used to generate and update static files:
The Code is as follows: |
Copy code |
<? Php // Form processing operation Header ("content-type: text/html; charset = UTF-8 "); Require_once 'conn. php '; $ Title = $ _ POST ['title']; $ Content = $ _ POST ['content']; $ Time = time (); If ($ _ POST ['submit '] = 'add '){ $ SQL = "insert into news values ('', '$ title',' $ content', $ time )"; $ Dbh-> query ($ SQL ); $ Id = $ dbh-> lastInsertId (); $ Filename = "news_registry.id=.html "; $ Fp_tmp = fopen ("template.html", "r "); $ Fp_html = fopen ($ filename, "w "); While (! Feof ($ fp_tmp )){ $ Row = fgets ($ fp_tmp ); $ Row = replace ($ row, $ title, $ content, date ('Y-m-d H: I: s', $ time )); Fwrite ($ fp_html, $ row ); } Fclose ($ fp_tmp ); Fclose ($ fp_html ); Echo "added successfully and generated static files "; } Else { $ SQL = "update news set title = $ title, content = $ content, time = $ time where id ={$ _ POST ['id']}"; $ Dbh-> query ($ SQL ); $ Filename = "news_00000000_post0000'id'00000000.html "; @ Unlink ($ filename ); $ Fp_tmp = fopen ("template.html", "r "); $ Fp_html = fopen ($ filename, "w "); While (! Feof ($ fp_tmp )){ $ Row = fgets ($ fp_tmp ); $ Row = replace ($ row, $ title, $ content, date ('Y-m-d H: I: s', $ time )); Fwrite ($ fp_html, $ row ); } Fclose ($ fp_tmp ); Fclose ($ fp_html ); Echo "the update is successful and the static file is updated "; } // Replace functions row by row Function replace ($ row, $ title, $ content, $ time ){ $ Row = str_replace ("{title}", $ title, $ row ); $ Row = str_replace ("{content}", $ content, $ row ); $ Row = str_replace ("{time}", $ time, $ row ); Return $ row; } ?>
|
In this way, a complete php generation Static Page system is complete.