当前位置:Gxlcms > PHP教程 > php快速url重写更新版[需php5.30以上]_PHP教程

php快速url重写更新版[需php5.30以上]_PHP教程

时间:2021-07-01 10:21:17 帮助过:11人阅读

对于apache的rewrite模块打开和设置则非本文主题,请见其他文章详解.
这个类只能php 5.30以上的版本才能使用,继承了上一个版本的快速重定向的特点(单独类,全部使用静态调用),增添了一个很重要的功能和属性 可以调用其他url中的模块了 也使得模块与模块间或页面与页面间的函数简化共享得以实现
.htaccess文件写法:
代码如下:
  1. <br>#-------------- .htaccess start --------------- <br>RewriteEngine on <br>RewriteRule !\.(js|ico|gif|jpg|png|css|swf|htm|txt)$ index.php <br>php_flag magic_quotes_gpc off <br>php_flag register_globals off <br>#-------------- .htaccess end --------------- <br> <br>重写功能引入:让站点根目录的index.php末尾写上下列代码,重写就开启了(正常条件:1.apache的重写配置成功,且开启了.htaccess支持的.2.站点根目录的.htaccess文件设置好了.3.class.rewrite.php类文件在index.php前面部分加载了.4.页面模块文件位置及写法无误): <br><span style="CURSOR: pointer" onclick="doCopy('code52547')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>//............ <br>Rewrite::__config( <br>$config['path'],/*'http://xxxxx/mysite/'URL基础位置*/ <br>$config['md_path'],/*'c:/phpsite/www/mysite/modules/'模块文件物理目录*/ <br>array( <br>'phpinfo' <br>) <br>); <br>Rewrite::__parse(); <br>//.......... <br> <br>模块文件写法: <br>testPk.php <br><span style="CURSOR: pointer" onclick="doCopy('code30521')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->class Rw_testPk extends Rewrite { <br>//这个是前导函数,只要访问到testpk这个页面,这个必然会执行,可用来控制本页面内函数访问权限或本页面全局变量 <br>public static function init(){ <br>//if (!defined('SITE_PASS')){ <br>echo self::$linktag.'<br>';//self::$linktag是页面解析位置路径值,会常使用. <br>//} <br>} <br>//当访问"http://localhost/testpk/"时会执行 <br>public static function index(){ <br>echo 'test'; <br>} <br>//当访问"http://localhost/testpk/blank"时会执行或写作"http://localhost/testpk/index/blank"一般"index/"都是可以被省略的 <br>public static function blank(){} <br>} <br>?> <br> <br>class.rewrite.php; <br><span style="CURSOR: pointer" onclick="doCopy('code70042')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->class Rewrite{ <br>public static $debug = false;//是否打开调试 <br>public static $time_pass = 0;//获得模块文件整体执行时间 <br>public static $version = 2.2; <br>public static $pretag = 'Rw_';//模块文件类的名称前缀 <br>public static $linktag = 'index';//页面链接标记,用来标记解析的是那个链接,可用来控制各种菜单效果和链接访问权限 <br>protected static $time_start = 0; <br>protected static $time_end = 0; <br>protected static $physical_path = '';//模块文件的物理路径 <br>protected static $website_path = '';//模块文件的站点路径,因为可能把站点放大站点的子目录下,如:http://localhost/site/mysite <br>protected static $ob_contents = ''; <br>protected static $uid = 0;//配合个人主页访问方式 如http://localhost/423/则是访问http://localhost/profile?uid=423 <br>//允许的系统函数如$allow_sys_fun=array('phpinfo')那么系统将允许链接访问phpinfo内容了,当http://localhost/phpinfo或http://localhost/......./phpinfo时就会直接执行phpinfo这个函数,不需要phpinfo.php模块文件 <br>private static $allow_sys_fun = array(); <br>private static function __get_microtime(){ <br>list($usec, $sec) = explode(" ",microtime()); <br>return ((float)$usec + (float)$sec); <br>} <br>//设置调试Rewrite::__debug(true); <br>public static function __debug($d = true){ <br>static::$debug = $d; <br>} <br>//配置路径和允许函数 <br>public static function __config($website_path = '',$physical_path = '',$allow_sys_fun = array()){ <br>self::$physical_path = $physical_path; <br>self::$website_path = $website_path; <br>self::$allow_sys_fun = $allow_sys_fun; <br>} <br>//调试函数 <br>public static function __msg($str){ <br>if(static::$debug){ <br>echo "\n<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>\n".print_r($str,true)."\n</li></ol></pre>\n"; <br>} <br>} <br>//解析开始时间 <br>public static function __start(){ <br>self::$time_start = self::__get_microtime(); <br>} <br>//解析结束时间 <br>public static function __end($re = false){ <br>self::$time_end = self::__get_microtime(); <br>self::$time_pass = round((self::$time_end - self::$time_start),6) * 1000; <br>if($re){ <br>return self::$time_pass; <br>}else{ <br>self::__msg('PASS_TIME: '.self::$time_pass.' ms'); <br>} <br>} <br>//内部跨模块url解析调用,如在test1.php模块页面中执行了Rwrite::__parseurl('/test2/show')这句,将调用test2.php模块页面中的show方法(Rw_test2这个class的方法) <br>public static function __parseurl($url = '',$fun = '',$data = NULL){ <br>if(!empty($url)&&!empty($fun)){ <br>$p = static::$physical_path; <br>if(file_exists($p.$url) || file_exists($p.$url.'.php') ){ <br>$part = strtolower(basename( $p.$url , '.php' )); <br>static::$linktag = $part.'/'.$fun; <br>$fname = static::$pretag.$part; <br>if(class_exists($fname, false)){ <br>if(method_exists($fname,$fun)){ <br>return $fname::$fun($data); <br>} <br>}else{ <br>include( $p.$url ); <br>if( class_exists($fname, false) && method_exists($fname,$fun)){ <br>return $fname::$fun($data); <br>} <br>} <br>} <br>} <br>} <br>//核心链接解析函数Rwrite::__parse();在顶级重写核心定向目标index.php中的执行,意味着Rwrite自定义重写开启 <br>public static function __parse($Url = ''){ <br>self::__start(); <br>$p = static::$physical_path; <br>$w = static::$website_path; <br>$req_execute = false; <br>$url_p = empty($Url) ? $_SERVER['REQUEST_URI'] : $Url; <br>$local = parse_url($w); <br>$req = parse_url($url_p); <br>$req_path = preg_replace('|[^\w/.\\\]|','',$req['path']); <br>$req_para = empty($Url) ? strstr($_SERVER['SERVER_NAME'],'.',true) : 'www'; <br>if(empty($Url) && substr_count($_SERVER['SERVER_NAME'],'.') == 2 && $req_para != 'www'){ <br>self::__goto($req_para,preg_replace('|^'.$local['path'].'|',"",$req_path)); <br>return ; <br>}else{ <br>$req_path_arr = empty($req_path)?array():preg_split("|[/\\\]+|",preg_replace('|^'.$local['path'].'|',"",$req_path)); <br>$req_fun = array_pop($req_path_arr); <br>if(substr($req_fun,0,2)=='__'){ <br>$req_fun = substr($req_fun,2); <br>} <br>$req_path_rearr = array_filter($req_path_arr); <br>self::__msg($req_path_rearr); <br>$req_temp = implode('/',$req_path_rearr); <br>$fname = $req_temp.'/'.$req_fun; <br>if(!empty($req_fun)&&in_array($req_fun,static::$allow_sys_fun)){ <br>$req_fun(); <br>}else{ <br>if(!empty($req_fun)&&file_exists($p.$fname.'.php') ){ <br>include( $p.$fname.'.php' ); <br>}else{ <br>$fname = empty($req_temp) ? 'index' : $req_temp; <br>if(file_exists($p.$fname.'.php') ){ <br>include( $p.$fname.'.php' ); <br>}else{ <br>$fname = $req_temp.'/index'; <br>if(file_exists($p.$fname.'.php')){ <br>include( $p.$fname.'.php' ); <br>}else{ <br>//这个地方是对"个人主页"的这种特殊链接定向到"profile/"了,可自己修改 <br>//如:www.xxx.com/12/将表示www.xxx.com/profile/?uid=12或www.xxx.com/profile?uid=12 <br>$uid = is_numeric($req_temp) ? $req_temp : strstr($req_temp, '/', true); <br>$ufun = is_numeric($req_temp) ? 'index' : strstr($req_temp, '/'); <br>if(is_numeric($uid)){ <br>self::$uid = $uid; <br>if(!isset($_GET['uid'])) $_GET['uid'] = $uid; <br>$fname = 'profile/'.$ufun; <br>if(file_exists($p.$fname.'.php')){ <br>include( $p.$fname.'.php' ); <br>}else{ <br>header("location:".$w); <br>exit(); <br>} <br>}else if(file_exists($p.'index.php')){ <br>$fname = 'index'; <br>include( $p.'index.php' ); <br>}else{ <br>header("location:".$w); <br>exit(); <br>} <br>} <br>} <br>} <br>$ev_fname = strrpos($fname,'/')===false ? $fname : substr($fname,strrpos($fname,'/')+1); <br>$ev_fname = static::$pretag.$ev_fname; <br>if( class_exists($ev_fname, false) && method_exists($ev_fname,$req_fun)){ <br>static::$linktag = $req_fun=='index' ? $fname.'/' : $fname.'/'.$req_fun; <br>if($req_fun != 'init' && method_exists($ev_fname,'init')){ <br>$ev_fname::init(); <br>} <br>$ev_fname::$req_fun(); <br>}else if( class_exists($ev_fname, false) && method_exists($ev_fname,'index') ){ <br>static::$linktag = $fname.'/'; <br>if(method_exists($ev_fname,'init')){ <br>$ev_fname::init(); <br>} <br>$ev_fname::index(); <br>}else if( $fname != 'index' && class_exists(static::$pretag.'index', false) && method_exists(static::$pretag.'index','index') ){ <br>$ev_fname = static::$pretag.'index'; <br>static::$linktag = 'index/'; <br>if(method_exists($ev_fname,'init')){ <br>$ev_fname::init(); <br>} <br>$ev_fname::index(); <br>}else{ <br>self::__msg('Function Not Exist!'); <br>} <br>} <br>} <br>self::__end(); <br>} <br>//这里是用户自定义链接的解析(用数据库存储的解析值) 如: xiaoming.baidu.com <br>//数据库中 xiaoming这个标签指向一个人的博客 就会到了www.baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(这个就看自己咋设计数据库了) <br>public static function __goto($para = '',$path = ''){ <br>$w = static::$website_path; <br>if(empty($para)){ <br>exit('未知链接,解析失败,不能访问'); <br>} <br>if(class_exists('Parseurl')){ <br>$prs = Parseurl::selectone(array('tag','=',$para)); <br>self::__msg($prs); <br>if(!empty($prs)){ <br>$parastr = $prs['tag']; <br>$output = array(); <br>$_GET[$prs['idtag']] = $prs['id']; <br>parse_str($prs['parastr'], $output); <br>$_GET = array_merge($_GET,$output); <br>$path = $prs['type'].'/'.preg_replace('|^/'.$prs['type'].'|','',$path); <br>self::__msg($path); <br>header('location:'.$w.$path.'?'.http_build_query($_GET)); <br>exit(); <br>}else{ <br>header("location:".$w); <br>exit(); <br>} <br>}else{ <br>header("location:".$w); <br>exit(); <br>} <br>} <br>} <br>?> <br></li><li><p></p></li><li><p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/321736.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/321736.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">对于apache的rewrite模块打开和设置则非本文主题,请见其他文章详解. 这个类只能php 5.30以上的版本才能使用,继承了上一个版本的快速重定向的...</span></p></li><li> </li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行