Two methods to implement pseudo-static in php
- /**
- * Php pseudo-static
- * Bbs.it-home.org
- */
- $ Conn = mysql_connect ("localhost", "root", "root") or dir ("connection failed ");
- Mysql_select_db ("tb_demo", $ conn );
- $ SQL = "select * from news ";
- $ Res = mysql_query ($ SQL );
- Header ("content-type: text/html; charset = utf-8 ");
- Echo "news list ";
- Echo "add news ";
- Echo"
- Echo"
- While ($ row = mysql_fetch_assoc ($ res )){
- }
- Echo"
- // The Red address above should have been show_news.php? Act = look & id = {$ row ['id']}
- Echo"
";
| Id |
Title |
View details |
Modify News |
";
| {$ Row ['id']} |
{$ Row ['title']} |
View details |
Modify page |
";
";
- // Close the resource
- Mysql_free_result ($ res );
- Mysql_close ($ conn );
- ?>
2. show_new.php page
- Header ("Content-type: text/html; charset = utf-8 ");
- $ Conn = mysql_connect ("localhost", "root", "root ");
- Mysql_select_db ("tb_demo", $ conn );
- Mysql_query ("set names utf8 ");
- $ Pa = $ _ SERVER ['path _ info'];
- // $ The value printed out by pa is/look-id-1.html
- // Use a regular expression to match the obtained url
- If (preg_match ('/^ \/(look)-(id)-([\ d]) \. shtml $/', $ pa, $ arr )){
- $ Act = $ arr [1]; // This is the look method of the request.
- $ Id = $ arr [3]; // This is the obtained id value.
- $ SQL = "select * from news where id = $ id ";
- $ Res = mysql_query ($ SQL );
- $ Res = mysql_fetch_assoc ($ res );
- Echo $ res ['title']. "". $ res ['content'];
- } Else {
- Echo "invalid url ";
- }
- Mysql_close ($ conn );
- ?>
2. according to the configuration. htaccess implementation first. create an htaccess file, create a notepad under the root directory of the website, and double-click it to open and click save as file name. htaccess: Select All files for the storage type, and select UTF-8 for encoding. you can see this in the directory. htaccess file. First, enable mod_rewrite.so in apache, and replace AllowOverride None with AllowOverride All. For example, the href address is written as a one_new-id-1.shtml // which means one_new.php? Id = 1 here. htaccess is written as follows:
-
- # Write your rewrite rules
- RewriteEngine On
- # You can configure multiple rules. The matching sequence is from top to bottom.
- RewriteRule one_new-id-(\ d +) \. shtml $ one_new.php? Id = $1 // Here $1 represents the first parameter.
- RewriteRule abc_id (\ d +) \. html $ error. php
- # Setting 404 errors
- # ErrorDocument 404/error. php
On the one_new.php page, echo $ _ GET ['id'] will certainly output the id value. Let's talk about this. it is very easy to implement pseudo-static in php. I hope you can practice more and write more flexible and lightweight pseudo-static rules. |