PHP implements pseudo-static code based on $_SERVER['PATH_INFO'] and .htaccess.

伊谢尔伦
Release: 2023-03-14 10:02:01
Original
2741 people have browsed it

1.根据$_SERVER['PATH_INFO']来操作实现。

  举个列子比如你的网站的地址是 http://127.0.0.1/show_new.php/look-id-1.shtml
你echo $_SERVER['PATH_INFO'] 出来的结果就会是 /look-id-1.shtml 看到这个我想大家可能已经明白了。
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 "

新闻列表

"; echo "添加新闻
"; echo ""; echo ""; while($row=mysql_fetch_assoc($res)){ echo ""; } //上面的红色的地址本来该是show_news.php?act=look&id={$row['id']} echo "
id标题查看详情修改新闻
{$row['id']}{$row['title']}查看详情修改页面
"; //关闭资源 mysql_free_result($res); mysql_close($conn);
Copy after login

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']."
".$res['content']; }else{ echo "url地址不合法"; } mysql_close($conn);
Copy after login

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

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 就可以这么写了


#写你的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
Copy after login

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

 

The above is the detailed content of PHP implements pseudo-static code based on $_SERVER['PATH_INFO'] and .htaccess.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!