php偽靜態的實現方式有以下幾種:
1.根據$_SERVER['PATH_INFO']來操作實現。
舉個列子比如你的網站的地址是 http://127.0.0.1/show_new.php/look-id-1.shtml
你echo $_SERVER['PATH_INFO'] 出來的結果就會是 /look-id-1.shtml 看到這個我想大家可能已 經明白了。
完整的demo
index.php
index.php
$conn=mysql_connect("localhost","root","root")or dir("
連接失敗");
mysql_select_db("tb_demo",$conn);
$sql="select * from news";
$res=mysql_query($sql);
header("content-type:text/html;charset=utf-8");
echo "<h1>新聞列表</h1>";
echo "<a href='add_news.html'>添加新聞</a><hr/>";
echo "<table>";
echo "<tr><td>id</td><td>標題</td><td>查看詳情
</td><td>修改新聞</td></tr>";
while($row=mysql_fetch_assoc($res)){
echo "<tr><td>{$row['id']}</td><td>{$row['title']}
</td><td><a href='show_new.php/look-id-{$row['id']}.shtml'>查看詳情
</a></td><td><a href='#'>修改頁面
</a></td></tr>";
}
//上面的紅色的地址本來該是show_news.php?act=look&id={$row['id']}
echo "</table>";
//關閉資源
mysql_free_result($res);
mysql_close($conn);
show_new.php頁面
show_new.php
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'];
//$pa 打印出來的值是 /look-id-1.html
//通過正則表達式匹配獲取的url地址
if(preg_match('/^\/(look)-(id)-([\d])\.shtml$/',$pa,$arr)){
$act = $arr[1]; //這個是請求的look方法
$id = $arr[3]; //這個是獲取的id 值
$sql="select * from news where id= $id";
$res=mysql_query($sql);
$res = mysql_fetch_assoc($res);
echo $res['title']."<hr>".$res['content'];
}else{
echo "url地址不合法";
}
mysql_close($conn);
看到上面的這個我想大家肯定懂了吧 其實這種方式用的不多的下面的給大家說第二種方法 了啊
查看本欄目