|
Only versions later than 5.30 can be used. it inherits the features of fast redirection in the previous version (separate classes, all use static calls) and adds a very important feature and attribute, you can call modules in other URLs to simplify the sharing of functions between modules or between pages .. Htaccess file syntax:
- # --------------. Htaccess start ---------------
- RewriteEngine on
- RewriteRule !. (Js | ico | gif | jpg | png | css | swf | htm | txt) $ index. php
- Php_flag magic_quotes_gpc off
- Php_flag register_globals off
- # --------------. Htaccess end ---------------
Rewrite function introduction: Let the index of the site root directory. write the following code at the end of php and rewrite the code (normal condition: 1. apache rewrite configuration is successful and enabled. htaccess supports. 2. the. the htaccess file has been set. 3. class. rewrite. php files are stored in index. php is loaded in the previous part. 4. the file location and writing method of the page module are correct): The code is as follows:
- //............
- Rewrite ::__ config (
- $ Config ['path'],/* 'http: // xxxxx/mysite/'URL base location */
- $ Config ['MD _ path'],/* 'C:/phpsite/www/mysite/modules/'physical Directory of the module file */
- Array (
- 'Phpinfo'
- )
- );
- Rewrite ::__ parse ();
- //..........
Module file syntax: testPk. php
- Class Rw_testPk extends Rewrite {
- // This is a leading function. if you access the testpk page, this function will be executed and can be used to control the function access permission or global variables on this page.
- Public static function init (){
- // If (! Defined ('site _ pass ')){
- Echo self: $ linktag .'
'; // Self: $ linktag is the path value of the page resolution location, which is often used.
- //}
- }
- // Executed when accessing "http://bbs.it-home.org/testpk/"
- Public static function index (){
- Echo 'test ';
- }
- // Execute or write "http://bbs.it-home.org/testpk/blank" when accessing "http://bbs.it-home.org/testpk/index/blank" generally "index/" can be omitted
- Public static function blank (){}
- }
- ?>
Class. rewrite. php; the code is as follows:
- Class Rewrite {
- Public static $ debug = false; // whether to enable debugging
- Public static $ time_pass = 0; // Obtain the overall execution time of the module file
- Public static $ version = 2.2;
- Public static $ pretag = 'rw _ '; // name prefix of the module file class
- Public static $ linktag = 'index'; // page link tag, which is used to mark the link to be parsed. it can be used to control various menu effects and link access permissions.
- Protected static $ time_start = 0;
- Protected static $ time_end = 0;
- Protected static $ physical_path = ''; // physical path of the module file
- Protected static $ website_path = ''; // The site path of the module file, because the site may be enlarged under the subdirectory of the site, such as: http://bbs.it-home.org/site/mysite
- Protected static $ ob_contents = '';
- Protected static $ uid = 0; // when used with the personal homepage access method, for example, http://bbs.it-home.org/423/, is the URL http://bbs.it-home.org/profile? Uid = 1, 423
- // For example, if $ allow_sys_fun = array ('phpinfo'), the system will allow the link to access the phpinfo content.
- Private static $ allow_sys_fun = array ();
- Private static function _ get_microtime (){
- List ($ usec, $ sec) = explode ("", microtime ());
- Return (float) $ usec + (float) $ sec );
- }
- // Set Rewrite ::__ debug (true );
- Public static function _ debug ($ d = true ){
- Static: $ debug = $ d;
- }
- // Configure the path and permitted functions
- Public static function _ config ($ website_path = '', $ physical_path ='', $ allow_sys_fun = array ()){
- Self: $ physical_path = $ physical_path;
- Self: $ website_path = $ website_path;
- Self: $ allow_sys_fun = $ allow_sys_fun;
- }
- // Debug the function
- Public static function _ msg ($ str ){
- If (static: $ debug ){
- Echo "n
n".print_r($str,true)."n N ";
- }
- }
- // Resolution start time
- Public static function _ start (){
- Self: $ time_start = self :__ get_microtime ();
- }
- // Resolution end time
- Public static function _ end ($ re = false ){
- Self: $ time_end = self :__ get_microtime ();
- Self: $ time_pass = round (self: $ time_end-self: $ time_start), 6) * 1000;
- If ($ re ){
- Return self: $ time_pass;
- } Else {
- Self ::__ msg ('pass _ TIME: '. self ::$ time_pass. 'Ms ');
- }
- }
- // Internal cross-module url resolution call. for example, if Rwrite ::__ parseurl ('/test2/Show') is executed on the test1.php module page, the show method in the test2.php module page will be called (the method of Rw_test2 class)
- Public static function _ parseurl ($ url = '', $ fun ='', $ data = NULL ){
- If (! Empty ($ url )&&! Empty ($ fun )){
- $ P = static: $ physical_path;
- If (file_exists ($ p. $ url) | file_exists ($ p. $ url. '. php ')){
- $ Part = strtolower (basename ($ p. $ url, '. php '));
- Static: $ linktag = $ part. '/'. $ fun;
- $ Fname = static: $ pretag. $ part;
- If (class_exists ($ fname, false )){
- If (method_exists ($ fname, $ fun )){
- Return $ fname: $ fun ($ data );
- }
- } Else {
- Include ($ p. $ url );
- If (class_exists ($ fname, false) & method_exists ($ fname, $ fun )){
- Return $ fname: $ fun ($ data );
- }
- }
- }
- }
- }
- // Core link parsing function Rwrite ::__ parse (); execute in top-level rewrite core targeting target index. php, which means that Rwrite custom rewriting is enabled
- Public static function _ parse ($ Url = ''){
- Self: :__ start ();
- $ P = static: $ physical_path;
- $ W = static: $ website_path;
- $ Req_execute = false;
- $ Url_p = empty ($ Url )? $ _ SERVER ['request _ URI ']: $ Url;
- $ Local = parse_url ($ w );
- $ Req = parse_url ($ url_p );
- $ Req_path = preg_replace ('| [^ w/.] |', '', $ req ['path']);
- $ Req_para = empty ($ Url )? Strstr ($ _ SERVER ['server _ name'], '.', true): 'WWW ';
- If (empty ($ Url) & substr_count ($ _ SERVER ['server _ name'], '.') = 2 & $ req_para! = 'WWW '){
- Self: :__ goto ($ req_para, preg_replace ('| ^'. $ local ['path']. '|', "", $ req_path ));
- Return;
- } Else {
- $ Req_path_arr = empty ($ req_path )? Array (): preg_split ("| [/] + |", preg_replace ('| ^ '. $ local ['path']. '|', "", $ req_path ));
- $ Req_fun = array_pop ($ req_path_arr );
- If (substr ($ req_fun, 0, 2) = '__'){
- $ Req_fun = substr ($ req_fun, 2 );
- }
- $ Req_path_rearr = array_filter ($ req_path_arr );
- Self: :__ msg ($ req_path_rearr );
- $ Req_temp = implode ('/', $ req_path_rearr );
- $ Fname = $ req_temp. '/'. $ req_fun;
- If (! Empty ($ req_fun) & in_array ($ req_fun, static: $ allow_sys_fun )){
- $ Req_fun ();
- } Else {
- If (! Empty ($ req_fun) & file_exists ($ p. $ fname. '. php ')){
- Include ($ p. $ fname. '. php ');
- } Else {
- $ Fname = empty ($ req_temp )? 'Index': $ req_temp;
- If (file_exists ($ p. $ fname. '. php ')){
- Include ($ p. $ fname. '. php ');
- } Else {
- $ Fname = $ req_temp. '/index ';
- If (file_exists ($ p. $ fname. '. php ')){
- Include ($ p. $ fname. '. php ');
- } Else {
- // This is a special link to the "personal homepage" and is directed to "profile/". you can modify it by yourself.
- // For example, www.xxx.com/12/www.xxx.com/profile /? Uid = 12 or www.xxx.com/profile? Uid = 12
- $ Uid = is_numeric ($ req_temp )? $ Req_temp: strstr ($ req_temp, '/', true );
- $ Ufun = is_numeric ($ req_temp )? 'Index': strstr ($ req_temp ,'/');
- If (is_numeric ($ uid )){
- Self: $ uid = $ uid;
- If (! Isset ($ _ GET ['uid']) $ _ GET ['uid'] = $ uid;
- $ Fname = 'Profile/'. $ ufun;
- If (file_exists ($ p. $ fname. '. php ')){
- Include ($ p. $ fname. '. php ');
- } Else {
- Header ("location:". $ w );
- Exit ();
- }
- } Else if (file_exists ($ p. 'index. php ')){
- $ Fname = 'index ';
- Include ($ p. 'index. php ');
- } Else {
- Header ("location:". $ w );
- Exit ();
- }
- }
- }
- }
- $ Ev_fname = strrpos ($ fname, '/') === false? $ Fname: substr ($ fname, strrpos ($ fname, '/') + 1 );
- $ Ev_fname = static: $ pretag. $ ev_fname;
- If (class_exists ($ ev_fname, false) & method_exists ($ ev_fname, $ req_fun )){
- Static: $ linktag = $ req_fun = 'index '? $ Fname. '/': $ fname. '/'. $ req_fun;
- If ($ req_fun! = 'Init '& method_exists ($ ev_fname, 'init ')){
- $ Ev_fname: init ();
- }
- $ Ev_fname: $ req_fun ();
- } Else if (class_exists ($ ev_fname, false) & method_exists ($ ev_fname, 'index ')){
- Static: $ linktag = $ fname .'/';
- If (method_exists ($ ev_fname, 'init ')){
- $ Ev_fname: init ();
- }
- $ Ev_fname: index ();
- } Else if ($ fname! = 'Index' & class_exists (static: $ pretag. 'index', false) & method_exists (static: $ pretag. 'index', 'index ')){
- $ Ev_fname = static: $ pretag. 'index ';
- Static: $ linktag = 'index /';
- If (method_exists ($ ev_fname, 'init ')){
- $ Ev_fname: init ();
- }
- $ Ev_fname: index ();
- } Else {
- Self: :__ msg ('function Not Exist! ');
- }
- }
- }
- Self: :__ end ();
- }
- // Here is the resolution of the custom link (the resolution value stored in the database), for example: xiaoming.baidu.com
- // In the database, when the xiaoming label points to a person's blog, it will go to www.baidu.com/blog? Uid = 12 or www.baidu.com/blog? Uname = xiaoming (this depends on how you design the database)
- Public static function _ goto ($ para = '', $ path = ''){
- $ W = static: $ website_path;
- If (empty ($ para )){
- Exit ('unknown link, resolution failed, cannot access ');
- }
- If (class_exists ('parseurl ')){
- $ Prs = Parseurl: selectone (array ('tag', '=', $ para ));
- Self: :__ msg ($ prs );
- If (! Empty ($ prs )){
- $ Parastr = $ prs ['tag'];
- $ Output = array ();
- $ _ GET [$ prs ['idtag'] = $ prs ['id'];
- Parse_str ($ prs ['parastr'], $ output );
- $ _ GET = array_merge ($ _ GET, $ output );
- $ Path = $ prs ['type']. '/'. preg_replace ('| ^ /'. $ prs ['type']. '|', '', $ path );
- Self: :__ msg ($ path );
- Header ('Location: '. $ w. $ path .'? '. Http_build_query ($ _ GET ));
- Exit ();
- } Else {
- Header ("location:". $ w );
- Exit ();
- }
- } Else {
- Header ("location:". $ w );
- Exit ();
- }
- }
- }
- ?>
|