当前位置:首页 > PHP教程 > PHP常见问题

php下实现伪 url 的超简单方法[转]

就像我的日志中的地址路径一样,让 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 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!