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);

看到上面的这个我想大家肯定懂了吧   其实这种方式用的不多的下面的给大家说第二种方法了啊

2.根据配置.htaccess来实现。
  先说下.htaccess文件怎么创建吧,在网站根目录下建立个记事本然后双击打开点击另存为 文件名写成
.htaccess ,保存类型选择所有文件,编码选择utf-8的编码好的这是你就在目录看到这个.htaccess文件了
 
首先在apache 开启mod_rewrite.so,AllowOverride None  这里有两处 替换为 AllowOverride All
 
比如href 地址写成 one_new-id-1.shtml //这个意思是one_new.php?id=1
这里的.htaccess 就可以这么写了

<IfModule rewrite_module>
#写你的rewrite规则
RewriteEngine On
# 可以配置多个规则,匹配的顺序是从上到下
RewriteRule  one_new-id-(\d+)\.shtml$ one_new.php?id=$1 //这里的$1 代表的是第一个参数啊
RewriteRule  abc_id(\d+)\.html$     error.php
#设置404错误
#ErrorDocument  404  /error.php
</IfModule>

 

你在one_new.php 页面echo $_GET['id'] 肯定会输出 id的值了

 说明:这个目前个人能力只能写到这里了 我以后会逐渐完善 的
   有问题可以给我留言啊

转载于:https://www.cnblogs.com/wlgaojin/archive/2012/09/06/2673221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值