For Apache rewrite module opening and setting is not the subject of this article, please see the other articles in detail.
This class can only be used by PHP 5.30 or more, inheriting the features of the fast redirection of the previous version (individual classes, all using static calls), adding a very important function and properties to invoke the modules in other URLs, and also make it possible to simplify the sharing of functions between modules and modules or between pages and pages.
The. htaccess file notation:
Copy CodeThe code is as follows:
#--------------. 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 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 the. 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 wording):
Copy CodeThe code is as follows:
//............
Rewrite::__config (
$config [' Path '],/* ' http://xxxxx/mysite/' URL base location */
$config [' Md_path '],/* ' c:/phpsite/www/mysite/modules/' module file physical directory */
Array
' Phpinfo '
)
);
Rewrite::__parse ();
//..........
Module file notation:
testpk.php
Copy CodeThe code is as follows:
Class Rw_testpk extends Rewrite {
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
public static function init () {
if (!defined (' Site_pass ')) {
echo Self:: $linktag. '
';//self:: $linktag is the page resolution location path value, which is often used.
//}
}
When access to "http://localhost/testpk/" is performed
public static function index () {
echo ' Test ';
}
When accessing "Http://localhost/testpk/blank" will be executed or written "Http://localhost/testpk/index/blank" General "index/" can be omitted
public static function blank () {}
}
?>
class.rewrite.php;
Copy CodeThe code is as follows:
Class rewrite{
public static $debug = false;//Whether debugging is turned on
public static $time _pass = 0;//Gets the overall execution time of the module file
public static $version = 2.2;
public static $pretag = ' rw_ ';//name prefix of module file class
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
protected static $time _start = 0;
protected static $time _end = 0;
protected static $physical _path = ";//physical path to the module file
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://localhost/site/mysite
protected static $ob _contents = ";
protected static $uid = 0;//with personal page access as http://localhost/423/is Access http://localhost/profile?uid=423
Allowed system functions such as $allow_sys_fun=array (' phpinfo ') then the system will allow links to access phpinfo content, when Http://localhost/phpinfo or http://localhost/ /phpinfo will execute the Phpinfo function directly without the need to phpinfo.php the module file.
private static $allow _sys_fun = Array ();
private static function __get_microtime () {
List ($usec, $sec) = Explode ("", Microtime ());
return (float) $usec + (float) $sec);
}
Set Debug Rewrite::__debug (True);
public static function __debug ($d = True) {
Static:: $debug = $d;
}
Configure paths and Allow 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;
}
debugging functions
public static function __msg ($STR) {
if (static:: $debug) {
echo "\ n
\ n ". Print_r ($str, true)." \ n
\ n ";
}
}
Parse Start time
public static function __start () {
Self:: $time _start = Self::__get_microtime ();
}
Parse 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 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)
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 (); Overriding the execution of the core-directed target index.php in the top-level, means that the Rwrite custom override is turned on
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 ():p reg_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 place is directed to "profile/" for this special link to "personal homepage" and can be modified by itself
such as: www.xxx.com/12/will represent 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 user-defined link (with the parsed value stored in the database) such as: xiaoming.baidu.com
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)
public static function __goto ($para = ', $path = ') {
$w = static:: $website _path;
if (empty ($para)) {
Exit (' Unknown link, parse 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 ();
}
}
}
?>
http://www.bkjia.com/PHPjc/321707.html www.bkjia.com true http://www.bkjia.com/PHPjc/321707.html techarticle for Apache rewrite module opening and setting is not the subject of this article, please see the other articles in detail. This class can only be used with PHP 5.30 or more, inheriting the previous version of the fast redirect ...