php下实现伪 url 的超简单方法[转]
程序员文章站
2022-08-29 18:34:35
就像我的日志中的地址路径一样,让 index.php?action=one&do=two 变成: ?index/action/one/do/...
就像我的日志中的地址路径一样,让 index.php?action=one&do=two
变成: ?index/action/one/do/two
index.php
--------------
<?php
// parsing query string
$qs=explode("&",$_server['query_string']);
$qs=explode('/',$qs[0]);
// if modul is undefined set it to index
if (!$qs[0]) $modul='index';
else $modul=strtolower($qs[0]);
// we can make a variable $_query
// for alternative _get
for ($i=1;$i<count($qs);$i+=2)
{
$_query[$nvar]=$nvar=$qs[$i];
$$nvar=$qs[$i+1];
}
// check the modul is exists?
if (!file_exists("modul_directory/{ $modul }.php"))
$modul="index";
#### this is example to implementation the script
// load the template
include("template.php");
// load the module
include("modul_directory/{ $modul }.php");
// load the footer
include("footer.php");
?>
we can access the modul in url like this:
=================================
www.example.com/?forum/topic/20
- it mean load the modul forum.php, and set the _query['topic']=20
www.foo.com/?voting/id/54/type/piechart&choice=2
- it mean load the modul voting.php, and set the _query['id']=54 and _query['type']='piechart' and set _get['choice']=2
变成: ?index/action/one/do/two
复制代码 代码如下:
index.php
--------------
<?php
// parsing query string
$qs=explode("&",$_server['query_string']);
$qs=explode('/',$qs[0]);
// if modul is undefined set it to index
if (!$qs[0]) $modul='index';
else $modul=strtolower($qs[0]);
// we can make a variable $_query
// for alternative _get
for ($i=1;$i<count($qs);$i+=2)
{
$_query[$nvar]=$nvar=$qs[$i];
$$nvar=$qs[$i+1];
}
// check the modul is exists?
if (!file_exists("modul_directory/{ $modul }.php"))
$modul="index";
#### this is example to implementation the script
// load the template
include("template.php");
// load the module
include("modul_directory/{ $modul }.php");
// load the footer
include("footer.php");
?>
we can access the modul in url like this:
=================================
www.example.com/?forum/topic/20
- it mean load the modul forum.php, and set the _query['topic']=20
www.foo.com/?voting/id/54/type/piechart&choice=2
- it mean load the modul voting.php, and set the _query['id']=54 and _query['type']='piechart' and set _get['choice']=2
上一篇: 一些常用的php简单命令代码集锦