


PHP fast url rewriting updated version [requires PHP 5.30 or above]_PHP tutorial
Opening and setting up Apache's rewrite module is not the subject of this article. Please see other articles for detailed explanations.
This class can only be used in versions above PHP 5.30 and inherits the fast redirection feature of the previous version (separate class, all using static calls), adding a very important function and attribute that can call modules in other URLs, and also enables simplified sharing of functions between modules or between pages.
.htaccess file Writing method:
#-------------. 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 ---------- -----
Introduction of rewriting function: write the following code at the end of index.php in the root directory of the site, and rewriting will be enabled (normal conditions: 1. Apache's rewrite configuration is successful, and .htaccess support is enabled. 2. The .htaccess file in the site root directory is set up. 3. The class.rewrite.php class file is loaded in the front part of index.php. 4. Page module The file location and writing are correct):
//...... ...
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 writing method:
testPk.php
class Rw_testPk extends Rewrite {
//This is the leading function , as long as the page testpk is accessed, this will definitely be executed and can be used to control access permissions to functions within this page or global variables of this page
public static function init(){
//if (!defined('SITE_PASS' )){
echo self::$linktag.'
';//self::$linktag is the page parsing location path value, which is often used.
//}
}
//When accessing "http://localhost/testpk/", it will be executed
public static function index(){
echo 'test';
}
//When accessing "http://localhost/testpk/blank", it will be executed or written as "http://localhost/testpk/index/blank". Generally, "index/" can be omitted
public static function blank(){}
}
?>
class.rewrite.php;
class Rewrite{
public static $debug = false;//Whether to turn on debugging
public static $time_pass = 0;//get Overall execution time of module file
public static $version = 2.2;
public static $pretag = 'Rw_';//Name prefix of module file class
public static $linktag = 'index'; //Page link tag, used to mark which link is being parsed, and 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 = '';//The physical path of the module file
protected static $website_path = '';//The site path of the module file, because the site may be enlarged to a subdirectory of the site, such as: http ://localhost/site/mysite
protected static $ob_contents = '';
protected static $uid = 0;//To access the personal homepage, such as http://localhost/423/, 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 When http://localhost/....../phpinfo, the phpinfo function will be executed directly without the 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 debugging Rewrite::__debug(true);
public static function __debug($d = true){
static::$debug = $d;
}
//Configuration path and allow function
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 function
public static function __msg( $str){
if(static::$debug){
echo "n
n".print_r($str,true)."nn";
}
}
//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 parsing call, such as executing Rwrite:: in the test1.php module page The sentence __parseurl('/test2/show') will call the show method in the test2.php module page (the method of the 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);
}
}
}
}
}
//The core link parsing function Rwrite::__parse(); is executed in the top-level rewrite core directional target index.php, which means that Rwrite custom rewriting 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():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 place directs the special link to "personal homepage" to "profile/", you can modify it yourself
//For example: www.xxx.com/12/ Indicates 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 parsing of the user-defined link (using the parsed value stored in the database) such as: xiaoming. baidu.com
//If the xiaoming tag in the database points to a person's blog, it will go to www.baidu.com/blog?uid=12 or www.baidu.com/blog?uname=xiaoming (this is It depends on how you design the database)
public static function __goto($para = '',$path = ''){
$w = static::$website_path;
if(empty( $para)){
exit('Unknown link, parsing failed, cannot be accessed');
}
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();
}
}
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
