PHP Quick URL Rewrite instance code

Source: Internet
Author: User
Tags php class

More than 5.30 of the version can be used, inherited the characteristics of the previous version of the fast redirection (separate classes, all using static calls), add a very important function and properties, can invoke other URLs in the module, but also the module and the module or the function of the page and the page to simplify the sharing can be implemented. htaccess file notation:

    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---------------
Copy Code

Rewrite feature introduction: Let the index.php at the end of the site root write the following code, the rewrite is turned on (normal condition: 1.apache rewrite configuration succeeded, and turned on. htaccess supported. 2. The. htaccess file for the site root is set. The 3.class.rewrite.php class file is loaded in the previous section of index.php. 4. Page module file location and correct format): The code is as follows:

    1. //............
    2. Rewrite::__config (
    3. $config [' Path '],/* ' http://xxxxx/mysite/' URL base location */
    4. $config [' Md_path '],/* ' c:/phpsite/www/mysite/modules/' module file physical directory */
    5. Array
    6. ' Phpinfo '
    7. )
    8. );
    9. Rewrite::__parse ();
    10. //..........
Copy Code

Module file notation: testpk.php

    1. Class Rw_testpk extends Rewrite {
    2. This is a leading function, as long as the access to TESTPK this page, this is bound to execute, can be used to control this page function access rights or the page global variables
    3. public static function init () {
    4. if (!defined (' Site_pass ')) {
    5. echo Self:: $linktag. '
      ';//self:: $linktag is the page resolution location path value, which is often used.
    6. //}
    7. }
    8. When access to "http://bbs.it-home.org/testpk/" is performed
    9. public static function index () {
    10. echo ' Test ';
    11. }
    12. When accessing "Http://bbs.it-home.org/testpk/blank" will be executed or written "Http://bbs.it-home.org/testpk/index/blank" General "index/" can be omitted
    13. public static function blank () {}
    14. }
    15. ?>
Copy Code

class.rewrite.php; code as follows:

  1. Class rewrite{
  2. public static $debug = false;//Whether debugging is turned on
  3. public static $time _pass = 0;//Gets the overall execution time of the module file
  4. public static $version = 2.2;
  5. public static $pretag = ' rw_ ';//name prefix of module file class
  6. public static $linktag = ' index ';//page link tag, which is used to mark the parsing of the link, which can be used to control various menu effects and link access rights
  7. protected static $time _start = 0;
  8. protected static $time _end = 0;
  9. protected static $physical _path = ";//physical path to the module file
  10. protected static $website _path = ";//The site path of the module file, as it may be the site to enlarge the subdirectory of the site, such as: Http://bbs.it-home.org/site/mysite
  11. protected static $ob _contents = ";
  12. protected static $uid = 0;//with personal page access as http://bbs.it-home.org/423/is Access http://bbs.it-home.org/profile?uid=423
  13. Allowed system functions such as $allow_sys_fun=array (' phpinfo ') then the system will allow links to access phpinfo content, when Http://bbs.it-home.org/phpinfo or http:// Bbs.it-home.org/......./phpinfo directly executes phpinfo This function, does not need phpinfo.php module file
  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. Set Debug Rewrite::__debug (True);
  20. public static function __debug ($d = True) {
  21. Static:: $debug = $d;
  22. }
  23. Configure paths and Allow functions
  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. Self:: $allow _sys_fun = $allow _sys_fun;
  28. }
  29. debugging functions
  30. public static function __msg ($STR) {
  31. if (static:: $debug) {
  32. echo "n
    N ". Print_r ($str, true)." N
    n ";
  33. }
  34. }
  35. Parse Start time
  36. public static function __start () {
  37. Self:: $time _start = Self::__get_microtime ();
  38. }
  39. Parse End time
  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. Internal cross-module URL resolution calls, such as executing rwrite::__parseurl ('/test2/show ') in the Test1.php module page, will invoke the Show method in the Test2.php module page (rw_ Test2 the method of this 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. Core link parsing function rwrite::__parse (); Overriding the execution of the core-directed target index.php in the top-level, means that the Rwrite custom override is turned on
  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 _p = Empty ($URL)? $_server[' Request_uri ': $Url;
  77. $local = Parse_url ($w);
  78. $req = Parse_url ($url _p);
  79. $req _path = preg_replace (' |[ ^w/.]| ', ', $req [' Path '];
  80. $req _para = Empty ($URL)? Strstr ($_server[' server_name '], '. ', true): ' www ';
  81. if (Empty ($URL) && substr_count ($_server[' server_name '), '. ') = = 2 && $req _para! = ' www ') {
  82. Self::__goto ($req _para,preg_replace (' |^ '. $local [' path ']. ' | ', "", $req _path));
  83. return;
  84. }else{
  85. $req _path_arr = Empty ($req _path)? Array ():p reg_split ("|[ /]+| ", Preg_replace (' |^ '. $local [' path ']. ' | '," ", $req _path));
  86. $req _fun = Array_pop ($req _path_arr);
  87. if (substr ($req _fun,0,2) = = ' __ ') {
  88. $req _fun = substr ($req _fun,2);
  89. }
  90. $req _path_rearr = Array_filter ($req _path_arr);
  91. Self::__msg ($req _path_rearr);
  92. $req _temp = implode ('/', $req _path_rearr);
  93. $fname = $req _temp. ' /'. $req _fun;
  94. if (!empty ($req _fun) &&in_array ($req _fun,static:: $allow _sys_fun)) {
  95. $req _fun ();
  96. }else{
  97. if (!empty ($req _fun) &&file_exists ($p. $fname. php ')) {
  98. Include ($p. $fname. php ');
  99. }else{
  100. $fname = Empty ($req _temp)? ' Index ': $req _temp;
  101. if (File_exists ($p. $fname. php ')) {
  102. Include ($p. $fname. php ');
  103. }else{
  104. $fname = $req _temp. ' /index ';
  105. if (File_exists ($p. $fname. php ')) {
  106. Include ($p. $fname. php ');
  107. }else{
  108. This place is directed to "profile/" for this special link to "personal homepage" and can be modified by itself
  109. such as: www.xxx.com/12/will represent www.xxx.com/profile/?uid=12 or 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 = static:: $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. Here is the resolution of the user-defined link (with the parsed value stored in the database) such as: xiaoming.baidu.com
  161. Database Xiaoming This tag to a person's blog will go to the www.baidu.com/blog?uid=12 or www.baidu.com/blog?uname=xiaoming (this depends on how you design the database)
  162. public static function __goto ($para = ', $path = ') {
  163. $w = static:: $website _path;
  164. if (empty ($para)) {
  165. Exit (' Unknown link, parse failed, cannot access ');
  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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.