欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)介紹

 更新時(shí)間:2013年04月28日 15:13:03   作者:  
本篇文章介紹了,基于php偽靜態(tài)的實(shí)現(xiàn)詳細(xì)分析。需要的朋友參考下

1.根據(jù)$_SERVER['PATH_INFO']來操作實(shí)現(xiàn)。
   舉個(gè)列子比如你的網(wǎng)站的地址是 http://127.0.0.1/show_new.php/look-id-1.shtml
你echo $_SERVER['PATH_INFO'] 出來的結(jié)果就會(huì)是 /look-id-1.shtml 看到這個(gè)我想大家可能已經(jīng)明白了。
完整的demo
index.php

復(fù)制代碼 代碼如下:

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>標(biāo)題</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>";
//關(guān)閉資源
mysql_free_result($res);
mysql_close($conn);


show_new.php頁面
復(fù)制代碼 代碼如下:

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
//通過正則表達(dá)式匹配獲取的url地址
if(preg_match('/^\/(look)-(id)-([\d])\.shtml$/',$pa,$arr)){
 $act = $arr[1]; //這個(gè)是請(qǐng)求的look方法
 $id = $arr[3];  //這個(gè)是獲取的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);


看到上面的這個(gè)我想大家肯定懂了吧   其實(shí)這種方式用的不多的下面的給大家說第二種方法了啊

2.根據(jù)配置.htaccess來實(shí)現(xiàn)。
  先說下.htaccess文件怎么創(chuàng)建吧,在網(wǎng)站根目錄下建立個(gè)記事本然后雙擊打開點(diǎn)擊另存為 文件名寫成
.htaccess ,保存類型選擇所有文件,編碼選擇utf-8的編碼好的這是你就在目錄看到這個(gè).htaccess文件了

首先在apache 開啟mod_rewrite.so,AllowOverride None  這里有兩處 替換為 AllowOverride All

比如href 地址寫成 one_new-id-1.shtml //這個(gè)意思是one_new.php?id=1
這里的.htaccess 就可以這么寫了

復(fù)制代碼 代碼如下:

<IfModule rewrite_module>
#寫你的rewrite規(guī)則
RewriteEngine On
# 可以配置多個(gè)規(guī)則,匹配的順序是從上到下
RewriteRule  one_new-id-(\d+)\.shtml$ one_new.php?id=$1 //這里的$1 代表的是第一個(gè)參數(shù)啊
RewriteRule  abc_id(\d+)\.html$     error.php
#設(shè)置404錯(cuò)誤
#ErrorDocument  404  /error.php
</IfModule>

你在one_new.php 頁面echo $_GET['id'] 肯定會(huì)輸出 id的值了

 說明:這個(gè)目前個(gè)人能力只能寫到這里了 我以后會(huì)逐漸完善 的
 有問題可以給我留言啊

相關(guān)文章

最新評(píng)論