PHP quick URL Rewrite update [php 5.30 +]_php Skills required

Source: Internet
Author: User
Tags php class php write phpinfo
For Apache rewrite modules to open and set up is not the subject of this article, please see the other article detailed.
This class can only be used in PHP version 5.30 or more. Inherited the features of the previous version of Fast redirection (separate classes, all using static calls), adding a very important feature and attribute that can invoke the modules in other URLs and make the function simplification sharing between modules and modules or pages and pages possible.
. htaccess file:
Copy Code code 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 site root directory at the end of the index.php write the following code, the rewrite is turned on (normal conditions: 1.apache rewrite configuration succeeded, and opened the. htaccess supported by. 2. The. htaccess file of the site root is set up. The 3.class.rewrite.php class file is loaded in the front section of index.php. 4. Page module file location and the correct wording):
Copy Code code as follows:

//............
Rewrite::__config (
$config [' Path '],/* ' http://xxxxx/mysite/' url base position * *
$config [' Md_path '],/* ' c:/phpsite/www/mysite/modules/' module file physical directory * *
Array
' Phpinfo '
)
);
Rewrite::__parse ();
//..........

Module file writing:
testpk.php
Copy Code code as follows:

<?php
Class Rw_testpk extends Rewrite {
This is the leading function, as long as the access to the TESTPK this page, this is bound to execute, can be used to control the function access in this page or global variables on this page
public static function init () {
if (!defined (' Site_pass ')) {
echo Self:: $linktag. ' <br/>//self:: $linktag is the page resolution location path value that will be used frequently.
//}
}
When you access "http://localhost/testpk/", you perform
public static function index () {
echo ' Test ';
}
The "Http://localhost/testpk/index/blank" General "index/" can be omitted when the "Http://localhost/testpk/blank" is accessed or written.
public static function blank () {}
}
?>

class.rewrite.php;
Copy Code code as follows:

<?php
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_ ';//Module file class name prefix
public static $linktag = ' index ';//page link tag, which is used to mark resolution of the link that 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 module file
protected static $website _path = ';//module file's site path, because the site may be magnified by the site's subdirectory, such as: Http://localhost/site/mysite
protected static $ob _contents = ';
protected static $uid = 0;//with personal home page access methods such as http://localhost/423/is access to 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 This function directly when/phpinfo, do not need 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 Debug Rewrite::__debug (True);
public static function __debug ($d = True) {
Static:: $debug = $d;
}
Configuration 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;
}
Debug functions
public static function __msg ($STR) {
if (static:: $debug) {
echo "\n<pre>\n". Print_r ($str, true). " \n</pre>\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 the execution of 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);
}
}
}
}
}
The core link parsing function rwrite::__parse (), execution in the top-level override core-directed target index.php, means rwrite the custom rewrite to open
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", which can be modified by oneself
For example: 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 ();
}
This is the resolution of user-defined links (parsed values stored in the database) such as: xiaoming.baidu.com
Database Xiaoming This tag points 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 I designed the database)
public static function __goto ($para = ', $path = ') {
$w = static:: $website _path;
if (empty ($para)) {
Exit (' Unknown link, parse failed, inaccessible ');
}
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 ();
}
}
}
?>
Related Article

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.