PHP quick URL rewriting new version [Requires PHP 5.30 or above]

Source: Internet
Author: User

Opening and setting the rewrite module of Apache is not the topic of this Article. For more information, see Article Details.
This class can only be used in PHP 5.30 or later versions, and inherits the features of fast redirection in the previous version (separate classes, all use static calls ), A very important function and attribute can be added to call the modules in other URLs. This also enables simplified sharing of functions between modules or between pages.
. Htaccess File Syntax: CopyCode The 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 function Introduction: 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 Style of the page module are correct.) copy Code 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 module files */
array (
'phpinfo'
);
rewrite ::__ parse ();
//..........

Module File Syntax:
Testpk. phpCopy codeThe Code is as follows: <? 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. '<br/>'; // self: $ linktag is the path value of the page resolution location, which is often used.
//}
}
// Executed when "http: // localhost/testpk/" is accessed
Public static function index (){
Echo 'test ';
}
// When "http: // localhost/testpk/blank" is accessed, "http: // localhost/testpk/index/blank "general" index/"can be omitted
Public static function blank (){}
}
?>

Class. Rewrite. php; Copy code The Code is as follows: <? PHP
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: // localhost/site/mysite
Protected static $ ob_contents = '';
Protected static $ uid = 0; // when used with the personal homepage, for example, http: // localhost/423/, access http: // localhost/profile? Uid = 1, 423
// If the system function is $ allow_sys_fun = array ('phpinfo'), the system will allow the link to access the phpinfo content. When http: // localhost/phpinfo or http: // localhost /....... /phpinfo directly executes the phpinfo function without phpinfo. PHP module File
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 <PRE> \ n". print_r ($ STR, true). "\ n </PRE> \ 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 ();
}
}
}
?>

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.