欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 简单的路由解析 可以随意设置后缀(html,do,shtml,aspx等等)_PHP教程

程序员文章站 2022-03-20 23:17:40
...
[php]
if(isset($_SERVER['PATH_INFO'])){
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//获取路径信息(pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//获取$_GET['m']
$_GET['m']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
//获取$_GET['a]
$_GET['a']=(!emptyempty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i $_GET[$pathinfo[$i]]=$pathinfo[$i+1];
}
}else{
$_GET['m']=(!emptyempty($_GET['m'])?$_GET['m']:'index');
$_GET['a']=(!emptyempty($_GET['a'])?$_GET['a']:'index');
if($_SERVER["QUERY_STRING"]){
$m=$_GET['m'];
unset($_GET['m']); //去除数组的m
$a=$_GET['a'];
unset($_GET['a']); //去除数组的a

$query=http_build_query($_GET); //组成新的URL参数
//组合新的URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html"; //重点在这
header("Location:".$url);
}
}

if(isset($_SERVER['PATH_INFO'])){
$str=substr($_SERVER['PATH_INFO'],0,strpos($_SERVER['PATH_INFO'],'.'));
//获取路径信息(pathinfo)
$pathinfo=explode('/',trim($str,'/'));
//获取$_GET['m']
$_GET['m']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
//获取$_GET['a]
$_GET['a']=(!empty($pathinfo[0])?$pathinfo[0]:'index');
//将数组单元的开头移除
array_shift($pathinfo);
$num=count($pathinfo);
for ($i=0;$i $_GET[$pathinfo[$i]]=$pathinfo[$i+1];
}
}else{
$_GET['m']=(!empty($_GET['m'])?$_GET['m']:'index');
$_GET['a']=(!empty($_GET['a'])?$_GET['a']:'index');
if($_SERVER["QUERY_STRING"]){
$m=$_GET['m'];
unset($_GET['m']); //去除数组的m
$a=$_GET['a'];
unset($_GET['a']); //去除数组的a

$query=http_build_query($_GET); //组成新的URL参数
//组合新的URL
$url=$_SERVER['SCRIPT_NAME']."/{$m}/{$a}/".str_replace(array("&","="),"/",$query).".html"; //重点在这
header("Location:".$url);
}
}

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477962.htmlTechArticle[php] if(isset($_SERVER[PATH_INFO])){ $str=substr($_SERVER[PATH_INFO],0,strpos($_SERVER[PATH_INFO],.)); //获取路径信息(pathinfo) $pathinfo=explode(/,trim($str,/)); //获取$_...