ホームページ >バックエンド開発 >PHPチュートリアル >php5.3以降でURLを高速に書き換える方法

php5.3以降でURLを高速に書き換える方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-07-25 09:08:12975ブラウズ
  1. #------------- .htaccess start ---------------
  2. RewriteEngine on
  3. RewriteRule !.(js|ico| gif |jpg|png|css|swf|htm|txt)$index.php
  4. php_flagmagic_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:/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 (!define('SITE_PASS')){
  5. echo self::$linktag.'
    //}
  6. }

  7. //「http:」にアクセスしたときに実行されます。 //localhost/testpk/"

  8. public static functionindex(){
  9. echo 'test';
  10. }

  11. //「http://localhost/testpk/blank」にアクセスすると、 「http://localhost/testpk/index/blank」として実行または記述されます通常「index/」は省略可能

  12. public static functionblank(){}
  13. }
  14. ?>

コードをコピー

class.rewrite.php;

  1. class Rewrite{
  2. public static $debug = false;//デバッグをオンにするかどうか
  3. public static $time_pass = 0;// module file
  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') などのシステム関数が許可されている場合、システムは http://localhost/phpinfo または http の場合にリンクが phpinfo コンテンツにアクセスすることを許可します。 ://localhost/.... ../phpinfo の場合、phpinfo 関数は phpinfo.php モジュール ファイルを使用せずに直接実行されます。

  14. private static $allow_sys_fun = array();

  15. プライベート静的関数 __get_microtime(){

  16. list( $usec, $sec) =explode(" ",microtime());
  17. return ((float)$usec + (float)$sec);
  18. }

  19. //デバッグの設定 書き換え ::__debug(true);

  20. public static function __debug($d = true){
  21. static::$debug = $d;
  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. パブリック静的関数 __msg($str){
  30. if(static::$debug){
  31. echo "n
    n".print_r($str,true)."n
    n";
  32. }
  33. }

  34. //解析開始時間

  35. パブリック静的関数__start(){
  36. self::$time_start = self::__get_microtime();
  37. }

  38. //終了時刻を解析

  39. public static function __end($re = false){
  40. self: :$time_end = self::__get_microtime() ;
  41. self::$time_pass =round((self::$time_end - self::$time_start),6) * 1000;
  42. if($re){
  43. return self: :$time_pass;
  44. }else{
  45. self ::__msg('PASS_TIME: '.self::$time_pass.' ms');
  46. }
  47. }

  48. //内部クロスモジュールtest1.php モジュール ページなどの URL 解析呼び出し Rwrite::__parseurl('/test2/show') という文が実行されると、test2.php モジュール ページの show メソッド (Rw_test2 クラスのメソッド) が

  49. パブリック静的関数 __parseurl($url = '',$fun = '',$data = NULL){
  50. if(!empty($url)&&!empty($fun)){
  51. $p = static: :$physical_path;
  52. if(file_exists($p.$url) | | file_exists($p.$url.'.php') ){
  53. $part = strto lower(basename( $p.$url , '.php' ));
  54. static::$linktag = $part.'/' .$fun;
  55. $fname = static::$pretag.$part;
  56. if(class_exists($fname, false)){
  57. if(method_exists( $fname,$fun)){
  58. return $fname::$fun ($data);
  59. }
  60. }else{
  61. include( $p.$url );
  62. if( class_exists($fname, false) && method_exists( $fname,$fun)){
  63. return $fname::$fun ($data);
  64. }
  65. }
  66. }
  67. }
  68. }

  69. //コアリンク解析関数 Rwrite::トップレベルの書き換えコアでの __parse(); 対象となるターゲットの Index.php の実行は、Rwrite カスタム書き換えが有効であることを意味します

  70. public static function __parse($Url = ''){
  71. self::__start();
  72. $p = static ::$physical_path;
  73. $w = static::$website_path ;
  74. $req_execute = false;

  75. $url_p = empty($Url) $_SERVER['REQUEST_URI'] : $Url ? ;

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

  90. self::__msg($req_path_rearr);

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

  92. $fname = $ req_temp.'/'.$req_fun;
  93. if(!empty($req_fun)&&in_array($req_fun,static::$allow_sys_fun)){
  94. $req_fun() ;
  95. }else{
  96. if(!empty($req_fun)&&file_exists($p.$fname.'.php') ){
  97. include( $p.$fname.'.php' );
  98. }else{
  99. $fname = empty ($req_temp) ? 'インデックス' : $req_temp;
  100. if(file_exists($p.$fname.'.php') ){
  101. include( $p.$fname.'.php' );
  102. }else{
  103. $fname = $req_temp.'/index';
  104. if(file_exists($p.$fname.'.php')){
  105. include( $p.$fname.'.php' );
  106. }else{< /p>
  107. //これは、「profile/」に向けられた「個人ホームページ」への特別なリンクです。自分で変更できます

  108. //たとえば、 www.xxx.com/12/ は www を意味します。 xxx.com/profile/?uid=12 または www.xxx.com/profile?uid=12

  109. $uid = is_numeric($req_temp) $req_temp : strstr($req_temp, ' / ', true);

  110. $ufun = is_numeric($req_temp) ? 'index' : strstr($req_temp, '/');
  111. if(is_numeric($uid)){
  112. self::$uid = $uid; 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($ev_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 = ' インデックス/';
  147. if(method_exists($ev_fname,'init')){
  148. $ev_fname::init();
  149. }
  150. $ev_fname::index();
  151. }else{
  152. self::__msg('関数ではありません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. パブリック静的関数 __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. ?>

コードをコピー

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。