Php quick url rewriting instance

Source: Internet
Author: User
Tags goto php and phpinfo

Php tutorials can be used only in versions later than 5.30, inheriting 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:
Copy the code as follows:
# --------------. Htaccess start ---------------
RewriteEngine on
RewriteRule !. (Js | ico | gif | jpg | png | css tutorial | 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 ):
Copy the code as follows:
//............
Rewrite ::__ config (
$ Config ['path'],/* 'http: // xxxxx/mysite/'URL base location */
$ Config ['MD _ path'],/* 'C:/phps tutorial ite/www/mysite/modules/'module file physical directory */
Array (
'Phpinfo'
)
);
Rewrite ::__ parse ();
//..........

Module file syntax:
TestPk. php
Copy the code 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 accessing "http://www.111cn.net/testpk/"
Public static function index (){
Echo 'test ';
}
// Execute or write "http://www.111cn.net/testpk/blank" when accessing "http://www.111cn.net/testpk/index/blank" generally "index/" can be omitted
Public static function blank (){}
}
?>

Class. rewrite. php;
Copy the code 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://www.111cn.net/site/mysite
Protected static $ ob_contents = '';
Protected static $ uid = 0; // when used with the personal homepage for access, for example, http://www.111cn.net/423/, the URL is http://www.111cn.net/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 "<pre>". print_r ($ str, true). "</pre> ";
}
}
// 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 tutorial), 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.