php快速url重写更新版[需php 5.30以上]


Posted in PHP onApril 25, 2010

对于apache的rewrite模块打开和设置则非本文主题,请见其他文章详解.

这个类只能php 5.30以上的版本才能使用,继承了上一个版本的快速重定向的特点(单独类,全部使用静态调用),增添了一个很重要的功能和属性 可以调用其他url中的模块了 也使得模块与模块间或页面与页面间的函数简化共享得以实现

.htaccess文件写法:

#-------------- .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 ---------------

重写功能引入:让站点根目录的index.php末尾写上下列代码,重写就开启了(正常条件:1.apache的重写配置成功,且开启了.htaccess支持的.2.站点根目录的.htaccess文件设置好了.3.class.rewrite.php类文件在index.php前面部分加载了.4.页面模块文件位置及写法无误):

//............ Rewrite::__config( 
$config['path'],/*'http://xxxxx/mysite/'URL基础位置*/ 
$config['md_path'],/*'c:/phpsite/www/mysite/modules/'模块文件物理目录*/ 
array( 
'phpinfo' 
) 
); 
Rewrite::__parse(); 
//..........

模块文件写法:

testPk.php

<?php 
class Rw_testPk extends Rewrite { //这个是前导函数,只要访问到testpk这个页面,这个必然会执行,可用来控制本页面内函数访问权限或本页面全局变量 
public static function init(){ 
//if (!defined('SITE_PASS')){ 
echo self::$linktag.'<br/>';//self::$linktag是页面解析位置路径值,会常使用. 
//} 
} 
//当访问"http://localhost/testpk/"时会执行 
public static function index(){ 
echo 'test'; 
} 
//当访问"http://localhost/testpk/blank"时会执行或写作"http://localhost/testpk/index/blank"一般"index/"都是可以被省略的 
public static function blank(){} 
} 
?>

class.rewrite.php;
<?php class Rewrite{ 
public static $debug = false;//是否打开调试 
public static $time_pass = 0;//获得模块文件整体执行时间 
public static $version = 2.2; 
public static $pretag = 'Rw_';//模块文件类的名称前缀 
public static $linktag = 'index';//页面链接标记,用来标记解析的是那个链接,可用来控制各种菜单效果和链接访问权限 
protected static $time_start = 0; 
protected static $time_end = 0; 
protected static $physical_path = '';//模块文件的物理路径 
protected static $website_path = '';//模块文件的站点路径,因为可能把站点放大站点的子目录下,如:http://localhost/site/mysite 
protected static $ob_contents = ''; 
protected static $uid = 0;//配合个人主页访问方式 如http://localhost/423/则是访问http://localhost/profile?uid=423 
//允许的系统函数如$allow_sys_fun=array('phpinfo')那么系统将允许链接访问phpinfo内容了,当http://localhost/phpinfo或http://localhost/......./phpinfo时就会直接执行phpinfo这个函数,不需要phpinfo.php模块文件 
private static $allow_sys_fun = array(); 
private static function __get_microtime(){ 
list($usec, $sec) = explode(" ",microtime()); 
return ((float)$usec + (float)$sec); 
} 

//设置调试Rewrite::__debug(true); 
public static function __debug($d = true){ 
static::$debug = $d; 
} 
//配置路径和允许函数 
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; 
} 
//调试函数 
public static function __msg($str){ 
if(static::$debug){ 
echo "\n<pre>\n".print_r($str,true)."\n</pre>\n"; 
} 
} 
//解析开始时间 
public static function __start(){ 
self::$time_start = self::__get_microtime(); 
} 
//解析结束时间 
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'); 
} 
} 
//内部跨模块url解析调用,如在test1.php模块页面中执行了Rwrite::__parseurl('/test2/show')这句,将调用test2.php模块页面中的show方法(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); 
} 
} 
} 
} 
} 
//核心链接解析函数Rwrite::__parse();在顶级重写核心定向目标index.php中的执行,意味着Rwrite自定义重写开启 
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{ 
//这个地方是对"个人主页"的这种特殊链接定向到"profile/"了,可自己修改 
//如:www.xxx.com/12/将表示www.xxx.com/profile/?uid=12或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(); 
} 
//这里是用户自定义链接的解析(用数据库存储的解析值) 如: xiaoming.baidu.com 
//数据库中 xiaoming这个标签指向一个人的博客 就会到了www.baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(这个就看自己咋设计数据库了) 
public static function __goto($para = '',$path = ''){ 
$w = static::$website_path; 
if(empty($para)){ 
exit('未知链接,解析失败,不能访问'); 
} 
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(); 
} 
} 
} 
?>
PHP 相关文章推荐
PHP 加密解密内部算法
Apr 22 PHP
fleaphp crud操作之findByField函数的使用方法
Apr 23 PHP
php中关于codeigniter的xmlrpc的类在进行数据交换时的类型问题
Jul 03 PHP
从手册去理解分析PHP session机制
Jul 17 PHP
比较简单实用的PHP无限分类源码分享(思路不错)
Oct 13 PHP
php地址引用(php地址引用的效率问题)
Mar 23 PHP
php函数array_merge用法一例(合并同类数组)
Feb 03 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
Jun 18 PHP
如何使用FireFox插件FirePHP调试PHP
Jul 23 PHP
10个实用的PHP正则表达式汇总
Oct 23 PHP
PHP基于DOMDocument解析和生成xml的方法分析
Jul 17 PHP
PHP 裁剪图片
Mar 09 PHP
PHP CURL模拟GET及POST函数代码
Apr 25 #PHP
php file_get_contents函数轻松采集html数据
Apr 22 #PHP
国外比较好的几个的Php开源建站平台小结
Apr 22 #PHP
PHP最常用的ini函数分析 针对PHP.ini配置文件
Apr 22 #PHP
php allow_url_include的应用和解释
Apr 22 #PHP
php strcmp使用说明
Apr 22 #PHP
PHP 加密解密内部算法
Apr 22 #PHP
You might like
收音机指标测试方法及仪器
2021/03/01 无线电
PHP 和 MySQL 基础教程(四)
2006/10/09 PHP
PHP定时更新程序设计思路分享
2014/06/10 PHP
在php的yii2框架中整合hbase库的方法
2018/09/20 PHP
在TP5数据库中四个字段实现无限分类的示例
2019/10/18 PHP
Laravel框架源码解析之反射的使用详解
2020/05/14 PHP
jquery 指南/入门基础
2007/11/30 Javascript
用js生产批量批处理执行命令
2008/07/28 Javascript
JavaScript 以对象为索引的关联数组
2010/05/19 Javascript
JSON.stringify 语法实例讲解
2012/03/14 Javascript
js 使用form表单select类实现级联菜单效果
2012/12/19 Javascript
JS实现带缓冲效果打开、关闭、移动一个层的方法
2015/05/09 Javascript
JavaScript中数据结构与算法(四):串(BF)
2015/06/19 Javascript
浅谈JavaScript for循环 闭包
2016/06/22 Javascript
Bootstrap CSS布局之表格
2016/12/17 Javascript
jQuery选取所有复选框被选中的值并用Ajax异步提交数据的实例
2017/08/04 jQuery
javascript将list转换成树状结构的实例
2017/09/08 Javascript
vue和webpack项目构建过程常用的npm命令详解
2018/06/15 Javascript
详解从NodeJS搭建中间层再谈前后端分离
2018/11/13 NodeJs
微信小程序实现点赞业务
2021/02/10 Javascript
python分割和拼接字符串
2013/11/01 Python
在Python的Django框架中调用方法和处理无效变量
2015/07/15 Python
python提取包含关键字的整行数据方法
2018/12/11 Python
详解python实现数据归一化处理的方式:(0,1)标准化
2019/07/17 Python
Python实现随机取一个矩阵数组的某几行
2019/11/26 Python
python中doctest库实例用法
2020/12/31 Python
Jupyter Notebook 远程访问配置详解
2021/01/11 Python
HTML5中的autofocus(自动聚焦)属性介绍
2014/04/23 HTML / CSS
英国花园、DIY、电器和家居用品商店:Robert Dyas
2019/03/18 全球购物
澳大利亚领先的男装零售连锁店:Lowes
2020/08/07 全球购物
附答案的Java面试题
2012/11/19 面试题
杭州龙健科技笔试题.net部分笔试题
2016/01/24 面试题
师德师风学习材料
2014/12/19 职场文书
先进个人推荐材料
2014/12/29 职场文书
初中化学教学反思
2016/02/22 职场文书
windows server 2012安装FTP并配置被动模式指定开放端口
2022/06/10 Servers