麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > Apache > 正文

php快速url重寫實例

2024-08-27 18:21:57
字體:
來源:轉載
供稿:網友

5.30以上的版本才能使用,繼承了上一個版本的快速重定向的特點(單獨類,全部使用靜態調用),增添了一個很重要的功能和屬性 可以調用其他url中的模塊了 也使得模塊與模塊間或頁面與頁面間的函數簡化共享得以實現 

.htaccess文件寫法: 

  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 off  
  6. #-------------- .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:/phps教程ite/www/mysite/modules/'模塊文件物理目錄*/  
  5. array(  
  6. 'phpinfo'  
  7. )  
  8. );  
  9. Rewrite::__parse();  
  10. //.......... 

模塊文件寫法: testPk.php 

  1. <?php  
  2. class Rw_testPk extends Rewrite {  
  3. //這個是前導函數,只要訪問到testpk這個頁面,這個必然會執行,可用來控制本頁面內函數訪問權限或本頁面全局變量  
  4. public static function init(){  
  5. //if (!defined('SITE_PASS')){  
  6. echo self::$linktag.'<br/>';//self::$linktag是頁面解析位置路徑值,會常使用.  
  7. //}  
  8. }  
  9. //當訪問"http://www.companysz.com/testpk/"時會執行  
  10. public static function index(){  
  11. echo 'test';  
  12. }  
  13. //當訪問"http://www.companysz.com/testpk/blank"時會執行或寫作"http://www.companysz.com/testpk/index/blank"一般"index/"都是可以被省略的  
  14. public static function blank(){}  
  15. }  
  16. ?> 

class.rewrite.php; 

  1. <?php  
  2. class Rewrite{  
  3. public static $debug = false;//是否打開調試  
  4. public static $time_pass = 0;//獲得模塊文件整體執行時間  
  5. public static $version = 2.2;  
  6. public static $pretag = 'Rw_';//模塊文件類的名稱前綴  
  7. public static $linktag = 'index';//頁面鏈接標記,用來標記解析的是那個鏈接,可用來控制各種菜單效果和鏈接訪問權限  
  8. protected static $time_start = 0;  
  9. protected static $time_end = 0;  
  10. protected static $physical_path = '';//模塊文件的物理路徑  
  11. protected static $website_path = '';//模塊文件的站點路徑,因為可能把站點放大站點的子目錄下,如:http://www.phpfeisi.com/site/mysite  
  12. protected static $ob_contents = '';  
  13. protected static $uid = 0;//配合個人主頁訪問方式 如http://www.companysz.com/423/則是訪問http://www.companysz.com/profile?uid=423  
  14. //允許的系統函數如$allow_sys_fun=array('phpinfo')那么系統將允許鏈接訪問phpinfo內容了,當http://www.companysz.com/phpinfo或http://www.companysz.com/......./phpinfo時就會直接執行phpinfo這個函數,不需要phpinfo.php模塊文件  
  15. private static $allow_sys_fun = array();  
  16. private static function __get_microtime(){  
  17. list($usec$sec) = explode(" ",microtime());  
  18. return ((float)$usec + (float)$sec);  
  19. }  
  20. //設置調試Rewrite::__debug(true);  
  21. public static function __debug($d = true){  
  22. static::$debug = $d;  
  23. }  
  24. //配置路徑和允許函數  
  25. public static function __config($website_path = '',$physical_path = '',$allow_sys_fun = array()){  
  26. self::$physical_path = $physical_path;  
  27. self::$website_path = $website_path;  
  28. self::$allow_sys_fun = $allow_sys_fun;  
  29. }  
  30. //調試函數  
  31. public static function __msg($str){  
  32. if(static::$debug){  
  33. echo "n<pre>n".print_r($str,true)."n</pre>n";  
  34. }  
  35. }  
  36. //解析開始時間  
  37. public static function __start(){  
  38. self::$time_start = self::__get_microtime();  
  39. }  
  40. //解析結束時間  
  41. public static function __end($re = false){  
  42. self::$time_end = self::__get_microtime();  
  43. self::$time_pass = round((self::$time_end - self::$time_start),6) * 1000;  
  44. if($re){  
  45. return self::$time_pass;  
  46. }else{  
  47. self::__msg('PASS_TIME: '.self::$time_pass.' ms');  
  48. }  
  49. }  
  50. //內部跨模塊url解析調用,如在test1.php模塊頁面中執行了Rwrite::__parseurl('/test2/show')這句,將調用test2.php模塊頁面中的show方法(Rw_test2這個class的方法)  
  51. public static function __parseurl($url = '',$fun = '',$data = NULL){  
  52. if(!emptyempty($url)&&!emptyempty($fun)){  
  53. $p = static::$physical_path;  
  54. if(file_exists($p.$url) || file_exists($p.$url.'.php') ){  
  55. $part = strtolower(basename$p.$url , '.php' ));  
  56. static::$linktag = $part.'/'.$fun;  
  57. $fname = static::$pretag.$part;  
  58. if(class_exists($fname, false)){  
  59. if(method_exists($fname,$fun)){  
  60. return $fname::$fun($data);  
  61. }  
  62. }else{  
  63. include$p.$url );  
  64. ifclass_exists($fname, false) && method_exists($fname,$fun)){  
  65. return $fname::$fun($data);  
  66. }  
  67. }  
  68. }  
  69. }  
  70. }  
  71. //核心鏈接解析函數Rwrite::__parse();在頂級重寫核心定向目標index.php中的執行,意味著Rwrite自定義重寫開啟  
  72. public static function __parse($Url = ''){  
  73. self::__start();  
  74. $p = static::$physical_path;  
  75. $w = static::$website_path;  
  76. $req_execute = false;  
  77. $url_p = emptyempty($Url) ? $_SERVER['REQUEST_URI'] : $Url;  
  78. $local = parse_url($w);  
  79. $req = parse_url($url_p);  
  80. $req_path = preg_replace('|[^w/./]|','',$req['path']);  
  81. $req_para = emptyempty($Url) ? strstr($_SERVER['SERVER_NAME'],'.',true) : 'www';  
  82. if(emptyempty($Url) && substr_count($_SERVER['SERVER_NAME'],'.') == 2 && $req_para != 'www'){  
  83. self::__goto($req_para,preg_replace('|^'.$local['path'].'|',"",$req_path));  
  84. return ;  
  85. }else{  
  86. $req_path_arr = emptyempty($req_path)?array():preg_split("|[//]+|",preg_replace('|^'.$local['path'].'|',"",$req_path));  
  87. $req_fun = array_pop($req_path_arr);  
  88. if(substr($req_fun,0,2)=='__'){  
  89. $req_fun = substr($req_fun,2);  
  90. }  
  91. $req_path_rearr = array_filter($req_path_arr);  
  92. self::__msg($req_path_rearr);  
  93. $req_temp = implode('/',$req_path_rearr);  
  94. $fname = $req_temp.'/'.$req_fun;  
  95. if(!emptyempty($req_fun)&&in_array($req_fun,static::$allow_sys_fun)){  
  96. $req_fun();  
  97. }else{  
  98. if(!emptyempty($req_fun)&&file_exists($p.$fname.'.php') ){  
  99. include$p.$fname.'.php' );  
  100. }else{  
  101. $fname = emptyempty($req_temp) ? 'index' : $req_temp;  
  102. if(file_exists($p.$fname.'.php') ){  
  103. include$p.$fname.'.php' );  
  104. }else{  
  105. $fname = $req_temp.'/index';  
  106. if(file_exists($p.$fname.'.php')){  
  107. include$p.$fname.'.php' );  
  108. }else{  
  109. //這個地方是對"個人主頁"的這種特殊鏈接定向到"profile/"了,可自己修改  
  110. //如:www.xxx.com/12/將表示www.xxx.com/profile/?uid=12或www.xxx.com/profile?uid=12  
  111. $uid = is_numeric($req_temp) ? $req_temp : strstr($req_temp'/', true);  
  112. $ufun = is_numeric($req_temp) ? 'index' : strstr($req_temp'/');  
  113. if(is_numeric($uid)){  
  114. self::$uid = $uid;  
  115. if(!isset($_GET['uid'])) $_GET['uid'] = $uid;  
  116. $fname = 'profile/'.$ufun;  
  117. if(file_exists($p.$fname.'.php')){  
  118. include$p.$fname.'.php' );  
  119. }else{  
  120. header("location:".$w);  
  121. exit();  
  122. }  
  123. }else if(file_exists($p.'index.php')){  
  124. $fname = 'index';  
  125. include$p.'index.php' );  
  126. }else{  
  127. header("location:".$w);  
  128. exit();  
  129. }  
  130. }  
  131. }  
  132. }  
  133. $ev_fname = strrpos($fname,'/')===false ? $fname : substr($fname,strrpos($fname,'/')+1);  
  134. $ev_fname = static::$pretag.$ev_fname;  
  135. ifclass_exists($ev_fname, false) && method_exists($ev_fname,$req_fun)){  
  136. static::$linktag = $req_fun=='index' ? $fname.'/' : $fname.'/'.$req_fun;  
  137. if($req_fun != 'init' && method_exists($ev_fname,'init')){  
  138. $ev_fname::init();  
  139. }  
  140. $ev_fname::$req_fun();  
  141. }else ifclass_exists($ev_fname, false) && method_exists($ev_fname,'index') ){  
  142. static::$linktag = $fname.'/';  
  143. if(method_exists($ev_fname,'init')){  
  144. $ev_fname::init();  
  145. }  
  146. $ev_fname::index();  
  147. }else if$fname != 'index' && class_exists(static::$pretag.'index', false) && method_exists(static::$pretag.'index','index') ){  
  148. $ev_fname = static::$pretag.'index';  
  149. static::$linktag = 'index/';  
  150. if(method_exists($ev_fname,'init')){  
  151. $ev_fname::init();  
  152. }  
  153. $ev_fname::index();  
  154. }else{  
  155. self::__msg('Function Not Exist!');  
  156. }  
  157. }  
  158. }  
  159. self::__end();  
  160. }  
  161. //這里是用戶自定義鏈接的解析(用數據庫教程存儲的解析值) 如: xiaoming.baidu.com  
  162. //數據庫中 xiaoming這個標簽指向一個人的博客 就會到了www.baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(這個就看自己咋設計數據庫了)  
  163. public static function __goto($para = '',$path = ''){  
  164. $w = static::$website_path;  
  165. if(emptyempty($para)){  
  166. exit('未知鏈接,解析失敗,不能訪問');  
  167. }  
  168. if(class_exists('Parseurl')){  
  169. $prs = Parseurl::selectone(array('tag','=',$para));  
  170. self::__msg($prs);  
  171. if(!emptyempty($prs)){  
  172. $parastr = $prs['tag'];  
  173. $output = array();  
  174. $_GET[$prs['idtag']] = $prs['id'];  
  175. parse_str($prs['parastr'], $output);  
  176. $_GET = array_merge($_GET,$output);  
  177. $path = $prs['type'].'/'.preg_replace('|^/'.$prs['type'].'|','',$path);  
  178. self::__msg($path);  
  179. header('location:'.$w.$path.'?'.http_build_query($_GET));  
  180. exit();  
  181. }else{  
  182. header("location:".$w);  
  183. exit();  
  184. }  
  185. }else{  
  186. header("location:".$w);  
  187. exit();  
  188. }  
  189. }  
  190. }  
  191. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中午字幕无线码一区2020 | 永久免费av在线 | 一级一级一级毛片 | 制服丝袜日日夜夜 | 一级网站 | 国产精品区一区二区三区 | 欧美成人鲁丝片在线观看 | 成人免费在线播放 | 亚洲特黄妇女高潮 | 在线成人看片 | 黄色片免费在线 | av日韩一区二区 | 四虎久草 | 午夜视频你懂的 | 欧美日韩大片在线观看 | 一级做人爱c黑人影片 | 欧美a级在线免费观看 | 日韩精品中文字幕在线播放 | 国产精品成人久久久久a级 av电影在线免费 | 91成人免费视频 | 爽爽淫人综合网网站 | 国产亚洲综合一区二区 | 最新欧美精品一区二区三区 | 久久久久久久久久久国产精品 | 国产九九 | 中文字幕极速在线观看 | 亚洲成人在线视频网 | 亚洲成人精品国产 | 亚洲一级毛片 | 日韩精品中文字幕在线播放 | 福利在线播放 | 国产免费一区视频 | 国产一区二区影视 | fc2国产成人免费视频 | 国产精品久久久久久久久久久久久久久 | 久久久成人精品视频 | 国产亚洲精品综合一区91555 | 热@国产| xx53xx| 成人在线视频播放 | 欧美日韩精品一区二区三区蜜桃 |