当前位置:首页 > PHP实现伪静态的方法

PHP实现伪静态的方法

点击次数:1937  更新日期:2011-02-14
\n

方法一:

\n

<?php
\n//伪静态方法一
\n// localhost/php100/test.php?id|1@action|2
\n$Php2Html_FileUrl = $_SERVER["REQUEST_URI"];
\necho $Php2Html_FileUrl.”<br>”;
\n// /php100/test.php?id|1@action|2
\n$Php2Html_UrlString = str_replace(“?”,”",str_replace(“/”, “”, strrchr(strrchr($Php2Html_FileUrl, “/”),”?”)));
\necho $Php2Html_UrlString.”<br>”;
\n// id|1@action|2
\n$Php2Html_UrlQueryStrList = explode(“@”, $Php2Html_UrlString);
\nprint_r($Php2Html_UrlQueryStrList);
\n// Array ( [0] => id|1 [1] => action|2 ) echo “<br>”;
\nforeach($Php2Html_UrlQueryStrList as $Php2Html_UrlQueryStr) {
\n$Php2Html_TmpArray = explode(“|”, $Php2Html_UrlQueryStr);
\nprint_r($Php2Html_TmpArray);
\n// Array ( [0] => id [1] => 1 ) ; Array ( [0] => action [1] => 2 )
\necho “<br>”;
\n$_GET[$Php2Html_TmpArray[0]] = $Php2Html_TmpArray[1];
\n}
\n//echo ‘假静态:$_GET变量<br />’;
\nprint_r($_GET);
\n// Array ( [id|1@action|2] => [id] => 1 [action] => 2 ) echo “<br>”;
\necho “<hr>”;
\necho $_GET[id].”<br>”;
\n// 1 echo $_GET[action];
\n// 2
\n?>

\n

方法2:

\n

<?php
\n//伪静态方法二
\n// localhost/php100/test.php/1/2
\n$filename = basename($_SERVER[\'SCRIPT_NAME\']);
\necho $_SERVER[\'SCRIPT_NAME\'].”<br>”;// /php100/test.php
\necho $filename.”<br>”;// test.php
\nif(strtolower($filename)==’test.php’){
\nif(!empty($_GET[id])){
\n$id=intval($_GET[id]);
\necho $id.”<br>”;
\n$action=intval($_GET[action]);
\necho $action.”<br>”;
\n}else{
\n$nav=$_SERVER[\'REQUEST_URI\'];
\necho “1:”.$nav.”<br>”;// /php100/test.php/1/2
\n$script=$_SERVER[\'SCRIPT_NAME\'];
\necho “2:”.$script.”<br>”;// /php100/test.php
\n$nav=ereg_replace(“^$script”,”",urldecode($nav));
\necho $nav.”<br>”; // /1/2
\n$vars=explode(“/”,$nav);
\nprint_r($vars);// Array ( [0] => [1] => 1 [2] => 2 )
\necho “<br>”;
\n$id=intval($vars[1]);
\n$action=intval($vars[2]);
\n}
\necho $id.’&’.$action;
\n}
\n?>

\n

方法3:

\n

<?php
\n//伪静态方法三
\nfunction mod_rewrite(){
\nglobal $_GET;
\n$nav=$_SERVER["REQUEST_URI"];
\necho $nav.”<br>”;
\n$script_name=$_SERVER["SCRIPT_NAME"];
\necho $script_name.”<br>”;
\n$nav=substr(ereg_replace(“^$script_name”,”",urldecode($nav)),1);
\necho $nav.”<br>”;
\n$nav=preg_replace(“/^.ht(m){1}(l){0,1}$/”,”",$nav);//这句是去掉尾部的.html或.htm
\necho $nav.”<br>”;
\n$vars = explode(“/”,$nav);
\nprint_r($vars);
\necho “<br>”;
\nfor($i=0;$i<Count($vars);$i+=2){
\n$_GET["$vars[$i]“]=$vars[$i+1];
\n}
\nreturn $_GET;
\n}
\nmod_rewrite();
\n$year=$_GET["year"];//结果为’2006′
\necho $year.”<br>”;
\n$action=$_GET["action"];//结果为’_add’
\necho $action;
\n?>

\n

方法4:

\n

<?php
\n//伪静态方法四
\n//利用server变量 取得PATH_INFO信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
\nif(@$path_info =$_SERVER["PATH_INFO"]){
\n//正则匹配一下参数
\nif(preg_match(“/\\/(\\d+),(\\d+),(\\d+)\\.html/si”,$path_info,$arr_path)){
\n$gid=intval($arr_path[1]); //取得值 1
\n$sid=intval($arr_path[2]); //取得值100
\n$softid=intval($arr_path[3]); //取得值8630
\n}else die(“Path:Error!”);
\n//相当于soft.php?gid=1&sid=100&softid=8630
\n}else die(‘Path:Nothing!’);
\n?>

\n