PATH_INFO方法实现页面伪静态的函数

先贴代码

PHP代码
  1. <?php      
  2. /*函数url_parse用于解析url,使用时需要在前面定义    
  3. *define('PATH_INFO',true);      
  4. *需要解析时调用该函数,取值方法用回$_GET['xxx']即可      
  5. */     
  6. function url_parse()      
  7. {      
  8.     if (!(isset($_SERVER['PATH_INFO']) && defined('PATH_INFO'))) {      
  9.         return;      
  10.     }      
  11.     $url = substr($_SERVER['PATH_INFO'], 1);      
  12.     $url = str_replace(array("'", '"', '.htm', '.html'), array('', '', '', ''), $url);    
  13.     $url = explode('/', $url);      
  14.     $param_count = count($url);      
  15.     for ($i = 0; $i < $param_count$i += 2) {      
  16.         if (isset($url[$i + 1]) && !is_numeric($url[$i])) {      
  17.             $_GET[$url[$i]] = $url[$i + 1];      
  18.         }      
  19.     }      
  20.     unset($param_count$url$i);      
  21. }      
  22. ?>  

 

说明:

www.yogool.cn/index.php/category/ajax 。(ajax后面可加.htm)这样,用$_GET['category']即可取出ajax.

再如 www.yogool.cn/index.php/category/ajax/page/2.htm,这样用$_GET['page']可取到2;它的页面相当于www.yogool.cn/index.php?category=ajax&page=2

淡水以前也写过,不过不是没写成函数,且要考虑到图片和css路径的问题.

原文:http://www.yogool.cn/index.php?controller=Default&action=ShowArticle&aid=10

更新一下:

 

PHP代码
  1. <?php   
  2.   
  3.   
  4. /*函数url_parse用于解析url,使用时需要在前面定义  
  5. *define('PATH_INFO',true);  
  6. *需要解析时调用该函数,取值方法用回$_GET['xxx']即可  
  7. */  
  8. function url_parse() {   
  9.     if (!(isset ($_SERVER['PATH_INFO']) && defined('PATH_INFO'))) {   
  10.         return;   
  11.     }   
  12.     $urlsubstr($_SERVER['PATH_INFO'], 1);   
  13.     $urlstr_replace(array (   
  14.         "'",   
  15.         '"',  
  16.         '.html',  
  17.         '.htm'  
  18.     ), array (  
  19.         '',  
  20.         '',  
  21.         '',  
  22.         ''  
  23.     ), $url);  
  24.     $url= explode('/', $url);  
  25.     $param_count= count($url);  
  26.     for ($i= 0; $i < $param_count; $i += 2) {  
  27.         if (isset ($url[$i +1]) && !is_numeric($url[$i])) {  
  28.             $_GET[$url[$i]]= $url[$i +1];  
  29.         }  
  30.     }  
  31.     unset ($param_count, $url, $i);  
  32. }  
  33. define('PATH_INFO', true);  
  34. url_parse();  
  35. header("Content-type:text/html;charset=utf-8");  
  36. echo "urlencode:";  
  37. echo urlencode($_GET['class']) . ',' . $_GET['news'];  
  38. echo '<br />urldecode:';  
  39. echo urldecode($_GET['class']) . ',' . $_GET['news'];  
  40. echo '<br />';  
  41. echo '<img src="/Public/Images/display.png" />';  
  42. echo '<br />';  
  43. echo '<a href="http://localhost/test.php/class/淡水/news/100.html">my link1</a>';   
  44. echo '<hr />';  
  45. echo '<a href="http://localhost/test.php/class/' . urlencode('淡水') . '/news/100.html">my link2</a>';   
  46. echo '<hr />';  
  47. echo '<a href="http://localhost/test.php?class=淡水&news=100">my link3</a>';   
  48. ?>  

注意点:原文代码里html在htm后面,这样地址为xxx.html的时候,会有一个L没替换到。

直接输入:http://localhost/test.php?class=淡水&news=100 中文会有乱码,点击连接却不会。其它都正常。怪事?!!!

Tags: path_info

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5

发表评论

评论内容 (必填):