Generate static page is PHP to reduce the server load and SEO site optimization a good choice, so php generated static page function is almost all PHP programmer must understand and master a knowledge point, I will introduce you to the PHP generation static page principle Analysis bar, there is need to know the friend can enter the reference.
Creating an HTML parsing principle
We write the label to create a template file, and then use PHP read to replace the specified label with the content we want to replace it, and now the mainstream DEDECMS system is doing so
Generate static page code.
The template is not yet populated with the content HTML file. For example:
|
copy code |
temp.html {title}</ title> </p> This is a {file} filearray;s templets templetest.php !--? p HP $title = "Maxtor International test Template"; $file = "Twomax Inter test Templet, 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; ? |
Such a super-simple PHP generation static page functionality is implemented, but the implementation of this is not practical, I would like to introduce a database to build an instance.
1. Create a test database, set up the user table as follows (insert several test databases yourself):
The code is as follows |
Copy Code |
CREATE TABLE IF not EXISTS ' news ' ( ' id ' int (ten) is not NULL auto_increment, ' title ' varchar (+) DEFAULT NULL, ' Content ' text, ' Time ' int (ten) DEFAULT NULL, PRIMARY KEY (' id ') ) Engine=innodb DEFAULT Charset=utf8 auto_increment=12;
|
2. Establish the connection data file conn.php
The code is as follows |
Copy Code |
$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 its connection is a static HTML connection, not yet generated, of course, the link is not open:
The code is as follows |
Copy Code |
Add an article
Require_once "conn.php"; $sql = "SELECT * FROM News"; foreach ($dbh->query ($sql) as $row) { echo "{$row [' title '}----Modify the article "; } ?> |
4. Add a modified article page:
The code is as follows |
Copy Code |
Get the modified content if ($_get[' id ')} { Require_once "conn.php"; $sql = "SELECT * from news where id={$_get[' id '}"; $res = $dbh->query ($sql)->fetch (); } ?>
|
5. page template for generating static files template.html
The code is as follows |
Copy Code |
{title}
{Title} published in {time}
{content}
|
6.action.php is, of course, used to generate and update static files:
The code is as follows |
Copy Code |
Form processing operations 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_{$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 "Add success and generate static files"; }else{ $sql = "Update news set title = $title, content = $content, time = $time where ID ={$_post[' ID '}"; $DBH->query ($sql); $filename = "news_{$_post[' id ']}.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 "Update succeeded and update static file"; } Progressive substitution function 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; } ?>
|
This completes the system of generating static pages for a full-native PHP.
http://www.bkjia.com/PHPjc/633071.html www.bkjia.com true http://www.bkjia.com/PHPjc/633071.html techarticle generating a static page is a good choice for PHP to reduce server load and SEO site optimization, so php generate static page functionality that almost all PHP programmers must know and master ...