搜尋
首頁後端開發php教程php快速url重寫實例程式碼

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_p #-------------- .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://bbs. it-home.org/testpk/"時會執行
  9. public static function index(){
  10. echo 'test';
  11. }
  12. //當造訪"http://bbs.it- home.org/testpk/blank"時會執行或寫作"http://bbs.it-home.org/testpk/index/blank"一般"index/"都是可以省略的
  13. public static function blank (){}
  14. }
  15. ?>
複製程式碼

class.rewrite.php; 代碼如下:

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


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何修改PHP會話中存儲的數據?您如何修改PHP會話中存儲的數據?Apr 27, 2025 am 12:23 AM

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

舉一個在PHP會話中存儲數組的示例。舉一個在PHP會話中存儲數組的示例。Apr 27, 2025 am 12:20 AM

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

垃圾收集如何用於PHP會議?垃圾收集如何用於PHP會議?Apr 27, 2025 am 12:19 AM

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。

如何在PHP中跟踪會話活動?如何在PHP中跟踪會話活動?Apr 27, 2025 am 12:10 AM

PHP中追踪用戶會話活動通過會話管理實現。 1)使用session_start()啟動會話。 2)通過$_SESSION數組存儲和訪問數據。 3)調用session_destroy()結束會話。會話追踪用於用戶行為分析、安全監控和性能優化。

如何使用數據庫存儲PHP會話數據?如何使用數據庫存儲PHP會話數據?Apr 27, 2025 am 12:02 AM

利用數據庫存儲PHP會話數據可以提高性能和可擴展性。 1)配置MySQL存儲會話數據:在php.ini或PHP代碼中設置會話處理器。 2)實現自定義會話處理器:定義open、close、read、write等函數與數據庫交互。 3)優化和最佳實踐:使用索引、緩存、數據壓縮和分佈式存儲來提升性能。

簡單地說明PHP會話的概念。簡單地說明PHP會話的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInAcookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionWwithSession_start()和stordoredAtain $ _session.2)

您如何循環中存儲在PHP會話中的所有值?您如何循環中存儲在PHP會話中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍歷會話數據可以通過以下步驟實現:1.使用session_start()啟動會話。 2.通過foreach循環遍歷$_SESSION數組中的所有鍵值對。 3.處理複雜數據結構時,使用is_array()或is_object()函數,並用print_r()輸出詳細信息。 4.優化遍歷時,可採用分頁處理,避免一次性處理大量數據。這將幫助你在實際項目中更有效地管理和使用PHP會話數據。

說明如何使用會話進行用戶身份驗證。說明如何使用會話進行用戶身份驗證。Apr 26, 2025 am 12:04 AM

會話通過服務器端的狀態管理機制實現用戶認證。 1)會話創建並生成唯一ID,2)ID通過cookies傳遞,3)服務器存儲並通過ID訪問會話數據,4)實現用戶認證和狀態管理,提升應用安全性和用戶體驗。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。