-
- /**
- * 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 "
id | title | View details | Modify news | ";
- while($row=mysql_fetch_assoc($res)){
- echo "
{$row['id']} | { $row['title']} | View details a> | Modify page | ";
- }
- //The red address above It should have been show_news.php?act=look&id={$row['id']}
- echo "
";
- //Close the resource
- mysql_free_result($res);
- mysql_close($conn);
- ?>
Copy code
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 printed value of $pa is /look-id-1.html
- //The url address obtained through regular expression matching
- if(preg_match('/^/(look)-(id)-([d]).shtml$/',$pa,$arr)){
- $ act = $arr[1]; //This is the requested look method
- $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 "url address is illegal";
- }
- mysql_close($conn);
- ?>
Copy the code
Second, implement it according to the configuration .htaccess
Let’s first talk about how to create the .htaccess file. Create a notepad in the root directory of the website and then double-click to open it. Click Save as and write the file name as .htaccess. Select all files as the save type and utf-8 as the encoding. This is what you need. I saw this .htaccess file in the directory.
First, open mod_rewrite.so in apache, and replace AllowOverride None in two places with AllowOverride All.
For example, the href address is written as one_new-id-1.shtml //This means one_new.php?id=1
The .htaccess here is written like this:
-
- #Write your rewrite rules
- RewriteEngine On
- # You can configure multiple rules, and the matching order is from top to bottom
- RewriteRule one_new-id-(d+).shtml$ one_new .php?id=$1 //$1 here represents the first parameter
- RewriteRule abc_id(d+).html$ error.php
- #Set 404 error
- #ErrorDocument 404 /error.php
-
Copy the code
In one_new.php page echo $_GET['id'] will definitely output the value of id.
Let’s just introduce these. It is very simple to implement pseudo-static in PHP. I hope you can practice more and write more flexible and lightweight pseudo-static rules.
|