url 重写 php示例,php快速url重写实例代码

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://bbs.it-home.org/site/mysite

protected static $ob_contents = '';

protected static $uid = 0;//配合个人主页访问方式 如http://bbs.it-home.org/423/则是访问http://bbs.it-home.org/profile?uid=423

//允许的系统函数如$allow_sys_fun=array('phpinfo')那么系统将允许链接访问phpinfo内容了,当http://bbs.it-home.org/phpinfo或http://bbs.it-home.org/......./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 "nn".print_r($str,true)."nn";

}

}

//解析开始时间

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();

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值