首頁  >  文章  >  後端開發  >  php5.3以上版本快速url重寫的方法

php5.3以上版本快速url重寫的方法

WBOY
WBOY原創
2016-07-25 09:08:12925瀏覽
  1. #-------------- .htaccess start ---------------
  2. RewriteEngine on
  3. RewriteRule !.(js|ico|gif|jpg|png|css|swf|htm|txt)$ index.php
  4. php_flag magic_quotes_gpc off
  5. php_flag register_globals-- ------------ .htaccess end ---------------
複製程式碼
重寫功能引入:讓站點根目錄的index.php末尾寫上下列程式碼,重寫就開啟了(正常條件:1.apache的重寫配置成功,且開啟了.htaccess支援的.2.站點根目錄的.htaccess檔設定好了.3.class.rewrite.php類別檔案在index.php前面部分載入了.4.頁面模組檔案位置及寫法無誤):

  1. //............
  2. Rewrite::__config(
  3. $config['path'] ,/*'http://xxxxx/mysite/'URL基礎位置*/
  4. $config['md_path'],/*'c:/phpsite/www/mysite/modules/'模組檔案實體目錄*/
  5. array(
  6. 'phpinfo'
  7. )
  8. );
  9. Rewrite::__parse();
  10. //..........
複製程式碼
模組檔案寫法:

testPk.php

  1. class Rw_testPk extends Rewrite {

  2. //這個是前導函數,只要訪問到testpk,這一點頁面,這個必然會執行,可用於控製本頁面內函數存取權限或本頁面全域變數
  3. public static function init(){
  4. //if (!defined('SITE_PASS')){
  5. echo self::$linktag.'
    ';//self::$linktag是頁面解析位置路徑值,會常使用.
  6. //}
  7. }
  8. //當造訪"http://localhost/testpk/"時會執行
  9. public static function index(){
  10. echo 'test';
  11. }
  12. //當造訪"http://localhost/testpk/blank"時會執行或寫作"http://localhost/testpk/index/blank"一般"index/"都是可以省略的

  13. public static function blank(){}
  14. }
  15. ?>
複製程式碼

class.rewrite.php;

  1. class Rewrite{

  2. public static $debug = false;//是否開啟偵錯
  3. publicstaticic $time_pass = 0;//取得模組檔案整體執行時間
  4. public static $version = 2.2;
  5. public static $pretag = 'Rw_';//模組檔案類別的名稱前綴
  6. public static $linktag = 'index';//頁面連結標記,用來標記解析的是那個連結,可用於控制各種選單效果和連結存取權

  7. protected static $time_start = 0;

  8. protected static $time_end = 0;
  9. protected static $physical_path = '';//模組檔案的實體路徑
  10. protected static $website_path = '';//模組檔案的網站路徑,因為可能把網站放大網站的子目錄下,如:http://localhost/site/mysite
  11. protected static $ob_contents = '';
  12. protected static $uid = 0;//配合個人首頁存取方式如http://localhost/423/則是存取http://localhost/profile?uid=423
  13. //允許的系統函數如$allow_sys_fun=array('phpinfo' )那麼系統將允許連結存取phpinfo內容了,當http://localhost/phpinfo或http://localhost/......./phpinfo時就會直接執行phpinfo這個函數,不需要phpinfo.php模組檔案

  14. private static $allow_sys_fun = array();
  15. private static function __get_microtime(){

  16. list($usec, $sec) = explode(" ",microtime() );
  17. return ((float)$usec + (float)$sec);
  18. }
  19. //設定偵錯Rewrite::__debug(true);

  20. public static function __debug($d = true){
  21. static::$debug = $d;
  22. }
  23. //設定路徑與允許函數

  24. public static function __config ($website_path = '',$physical_path = '',$allow_sys_fun = array()){
  25. self::$physical_path = $physical_path;
  26. self::$website_path = $website_path;}
  27. //調試函數

  28. public static function __msg($str){
  29. if(static::$debug){
  30. echo "n
    n".print_r($str,true)."n
    n";
  31. }
  32. }
  33. //解析開始時間

  34. public static function __start(){
  35. self::$time_start = self::__get_microtime();
  36. }
  37. //解析結束時間

  38. public static function __end($re = false){
  39. self::$time_end = self::__get_microtime();
  40. self::$time_pass = round((self::$time_end - self::$time_start) ,6) * 1000;
  41. if($re){
  42. return self::$time_pass;
  43. }else{
  44. self::__msg('PASS_TIME: '.self::$time_pass. ' ms');
  45. }
  46. }
  47. //內部跨模組url解析呼叫,如在test1.php模組頁面中執行了Rwrite::__parseurl('/test2 /show')這句話,將呼叫test2.php模組頁面中的show方法(Rw_test2這個class的方法)

  48. public static function __parseurl($url = '',$fun = '',$data = NULL) {
  49. if(!empty($url)&&!empty($fun)){
  50. $p = static::$physical_path;
  51. if(file_exists($p.$url) || file_exists( $p.$url.'.php') ){
  52. $part = strtolower(basename( $p.$url , '.php' ));
  53. static::$linktag = $part.'/ '.$fun;
  54. $fname = static::$pretag.$part;
  55. if(class_exists($fname, false)){
  56. if(method_exists($fname,$fun)){
  57. return $fname::$fun($data);
  58. }
  59. }else{
  60. include( $p.$url );
  61. if( class_exists($fname, false) && method_exists ($fname,$fun)){
  62. return $fname::$fun($data);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. //核心連結解析函數Rwrite::__parse();在頂級重寫核心定向目標index.php中的執行,意味著Rwrite自訂重寫開啟

  69. public static function __parse($Url = ''){
  70. self::__start();
  71. $p = static::$physical_path;
  72. $w = static::$website_path;
  73. $req_execute = false;
  74. $url_p = empty($Url) ? $_SERVER['REQUEST_URI'] : $Url;

  75. $local = parse_url($w);
  76. $req = parse_url($url_p);
  77. $req_path = preg_replace('|[^w/.\]|','',$req['path']);
  78. $req_para = empty($Url) ? strstr($_SERVER[' SERVER_NAME'],'.',true) : 'www';
  79. if(empty($Url) && substr_count($_SERVER['SERVER_NAME'],'.') == 2 && $req_para != 'www '){
  80. self::__goto($req_para,preg_replace('|^'.$local['path'].'|',"",$req_path));
  81. return ;
  82. } else{
  83. $req_path_arr = empty($req_path)?array():preg_split("|[/\]+|",preg_replace('|^'.$local['path'].'|'," ",$req_path));
  84. $req_fun = array_pop($req_path_arr);
  85. if(substr($req_fun,0,2)=='__'){
  86. $req_fun = substr($req_fun ,2);
  87. }
  88. $req_path_rearr = array_filter($req_path_arr);
  89. self::__msg($req_path_rearr);

  90. $ req_temp = implode('/',$req_path_rearr);

  91. $fname = $req_temp.'/'.$req_fun;
  92. if(!empty($req_fun)&&in_array($req_funstaticstatic; ){
  93. $req_fun();
  94. }else{
  95. if(!empty($req_fun)&&file_exists($p.$fname.'.php') ){
  96. include( $p.$fname.'.php' );
  97. }else {
  98. $fname = empty($req_temp) ? 'index' : $req_temp;
  99. if(file_exists($p.$fname.'.php') ){
  100. include( $p.$fname .'.php' );
  101. }else{
  102. $fname = $req_temp.'/index';
  103. if(file_exists($p.$fname.'.php')){
  104. include( $p.$fname.'.php' );
  105. }else{
  106. //這個地方是對"個人主頁"的這種特殊連結定向到"profile/ "了,可自行修改

  107. //如:www.xxx.com/12/將表示www.xxx.com/profile/?uid=12或www.xxx.com/profile?uid=12
  108. $uid = is_numeric($req_temp) ? $req_temp : strstr($req_temp, '/', true);

  109. $ufun = is_numeric($req_temp) ? 'index' : strstr($ req_temp, '/');
  110. if(is_numeric($uid)){
  111. self::$uid = $uid;
  112. if(!isset($_GET['uid'])) $_GET ['uid'] = $uid;
  113. $fname = 'profile/'.$ufun;
  114. if(file_exists($p.$fname.'.php')){
  115. include( $p .$fname.'.php' );
  116. }else{
  117. header("location:".$w);
  118. exit();
  119. }
  120. }else if(file_exists( $p.'index.php')){
  121. $fname = 'index';
  122. include( $p.'index.php' );
  123. }else{
  124. header("location: ".$w);
  125. exit();
  126. }
  127. }
  128. }
  129. }
  130. $ev_fname = strrpos($fname,'/')===false ? $ fname : substr($fname,strrpos($fname,'/')+1);
  131. $ev_fname = static::$pretag.$ev_fname;
  132. if( class_exists($ev_fname, false) && method_exists( $ev_fname,$req_fun)){
  133. static::$linktag = $req_fun=='index' ? $fname.'/' : $fname.'/'.$req_fun;
  134. if($req_fun ! = 'init' && method_exists($ev_fname,'init')){
  135. $ev_fname::init();
  136. }
  137. $ev_fname::$req_fun();
  138. }else if( class_exists($ev_fname, false) && method_exists($ev_fname,'index') ){
  139. static::$linktag = $fname.'/';
  140. if(method_exists($linktag_fname,'init'))) {
  141. $ev_fname::init();
  142. }
  143. $ev_fname::index();
  144. }else if( $fname != 'index' && class_exists(static::$pretag. 'index', false) && method_exists(static::$pretag.'index','index') ){
  145. $ev_fname = static::$pretag.'index';
  146. static::$linktag = 'index/';
  147. if(method_exists($ev_fname,'init')){
  148. $ev_fname::init();
  149. }
  150. $ev_fname::index();
  151. }else{
  152. self::__msg('Function Not Exist!');
  153. }
  154. }
  155. }
  156. self::__end();
  157. }
  158. //這裡是用戶自訂連結的解析(用資料庫儲存的解析值) 如: xiaoming.baidu.com

  159. //資料庫中xiaoming這個標籤指向一個人的部落格就會到了www. baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(看如何設計資料庫了)
  160. public static function __goto($para = '',$path = ''){
  161. $w = static::$website_path;
  162. if(empty($para)){
  163. exit('未知連結,解析失敗,不能存取');
  164. }
  165. if(class_exists ('Parseurl')){
  166. $prs = Parseurl::selectone(array('tag','=',$para));
  167. self::__msg($prs);
  168. if( !empty($prs)){
  169. $parastr = $prs['tag'];
  170. $output = array();
  171. $_GET[$prs['idtag']] = $prs[ 'id'];
  172. parse_str($prs['parastr'], $output);
  173. $_GET = array_merge($_GET,$output);
  174. $path = $prs['type'] .'/'.preg_replace('|^/'.$prs['type'].'|','',$path);
  175. self::__msg($path);
  176. header(' location:'.$w.$path.'?'.http_build_query($_GET));
  177. exit();
  178. }else{
  179. header("location:".$w);
  180. exit();
  181. }
  182. }else{
  183. header("location:".$w);
  184. exit();
  185. }
  186. }
  187. }
  188. }
  189. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn