热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php快速url重写实例

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

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

.htaccess文件写法: 

  1. #-------------- .htaccess start ---------------  
  2. RewriteEngine on  
  3. RewriteRule !.(js|ico|gif|jpg|png|css教程|swf|htm|txt)$ index.php  
  4. php_flag magic_quotes_gpc off  
  5. php_flag register_globals off  
  6. #-------------- .htaccess end --------------- 

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

  1. //............  
  2. Rewrite::__config(  
  3. $config['path'],/*'http://xxxxx/mysite/'URL基础位置*/  
  4. $config['md_path'],/*'c:/phps教程ite/www/mysite/modules/'模块文件物理目录*/  
  5. array(  
  6. 'phpinfo'  
  7. )  
  8. );  
  9. Rewrite::__parse();  
  10. //.......... 

模块文件写法: testPk.php 

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

class.rewrite.php; 

  1. class Rewrite{  
  2. public static $debug = false;//是否打开调试  
  3. public static $time_pass = 0;//获得模块文件整体执行时间  
  4. public static $version = 2.2;  
  5. public static $pretag = 'Rw_';//模块文件类的名称前缀  
  6. public static $linktag = 'index';//页面链接标记,用来标记解析的是那个链接,可用来控制各种菜单效果和链接访问权限  
  7. protected static $time_start = 0;  
  8. protected static $time_end = 0;  
  9. protected static $physical_path = '';//模块文件的物理路径  
  10. protected static $website_path = '';//模块文件的站点路径,因为可能把站点放大站点的子目录下,如:http://www.phpfeisi.com/site/mysite  
  11. protected static $ob_contents = '';  
  12. protected static $uid = 0;//配合个人主页访问方式 如http://www.phpfensi.com/423/则是访问http://www.phpfensi.com/profile?uid=423  
  13. //允许的系统函数如$allow_sys_fun=array('phpinfo')那么系统将允许链接访问phpinfo内容了,当http://www.phpfensi.com/phpinfo或http://www.phpfensi.com/......./phpinfo时就会直接执行phpinfo这个函数,不需要phpinfo.php模块文件  
  14. private static $allow_sys_fun = array();  
  15. private static function __get_microtime(){  
  16. list($usec$sec) = explode(" ",microtime());  
  17. return ((float)$usec + (float)$sec);  
  18. }  
  19. //设置调试Rewrite::__debug(true);  
  20. public static function __debug($d = true){  
  21. static::$debug = $d;  
  22. }  
  23. //配置路径和允许函数  
  24. public static function __config($website_path = '',$physical_path = '',$allow_sys_fun = array()){  
  25. self::$physical_path = $physical_path;  
  26. self::$website_path = $website_path;  
  27. self::$allow_sys_fun = $allow_sys_fun;  
  28. }  
  29. //调试函数  
  30. public static function __msg($str){  
  31. if(static::$debug){  
  32. echo "n
    n".print_r($str,true)."n
    n"
    ;  
  33. }  
  34. }  
  35. //解析开始时间  
  36. public static function __start(){  
  37. self::$time_start = self::__get_microtime();  
  38. }  
  39. //解析结束时间  
  40. public static function __end($re = false){  
  41. self::$time_end = self::__get_microtime();  
  42. self::$time_pass = round((self::$time_end - self::$time_start),6) * 1000;  
  43. if($re){  
  44. return self::$time_pass;  
  45. }else{  
  46. self::__msg('PASS_TIME: '.self::$time_pass.' ms');  
  47. }  
  48. }  
  49. //内部跨模块url解析调用,如在test1.php模块页面中执行了Rwrite::__parseurl('/test2/show')这句,将调用test2.php模块页面中的show方法(Rw_test2这个class的方法)  
  50. public static function __parseurl($url = '',$fun = '',$data = NULL){  
  51. if(!emptyempty($url)&&!emptyempty($fun)){  
  52. $p = static::$physical_path;  
  53. if(file_exists($p.$url) || file_exists($p.$url.'.php') ){  
  54. $part = strtolower(basename$p.$url , '.php' ));  
  55. static::$linktag = $part.'/'.$fun;  
  56. $fname = static::$pretag.$part;  
  57. if(class_exists($fname, false)){  
  58. if(method_exists($fname,$fun)){  
  59. return $fname::$fun($data);  
  60. }  
  61. }else{  
  62. include$p.$url );  
  63. ifclass_exists($fname, false) && method_exists($fname,$fun)){  
  64. return $fname::$fun($data);  
  65. }  
  66. }  
  67. }  
  68. }  
  69. }  
  70. //核心链接解析函数Rwrite::__parse();在顶级重写核心定向目标index.php中的执行,意味着Rwrite自定义重写开启  
  71. public static function __parse($Url = ''){  
  72. self::__start();  
  73. $p = static::$physical_path;  
  74. $w = static::$website_path;  
  75. $req_execute = false;  
  76. $url_p = emptyempty($Url) ? $_SERVER['REQUEST_URI'] : $Url;  
  77. $local = parse_url($w);  
  78. $req = parse_url($url_p);  
  79. $req_path = preg_replace('|[^w/.\]|','',$req['path']);  
  80. $req_para = emptyempty($Url) ? strstr($_SERVER['SERVER_NAME'],'.',true) : 'www';  
  81. if(emptyempty($Url) && substr_count($_SERVER['SERVER_NAME'],'.') == 2 && $req_para != 'www'){  
  82. self::__goto($req_para,preg_replace('|^'.$local['path'].'|',"",$req_path));  
  83. return ;  
  84. }else{  
  85. $req_path_arr = emptyempty($req_path)?array():preg_split("|[/\]+|",preg_replace('|^'.$local['path'].'|',"",$req_path));  
  86. $req_fun = array_pop($req_path_arr);  
  87. if(substr($req_fun,0,2)=='__'){  
  88. $req_fun = substr($req_fun,2);  
  89. }  
  90. $req_path_rearr = array_filter($req_path_arr);  
  91. self::__msg($req_path_rearr);  
  92. $req_temp = implode('/',$req_path_rearr);  
  93. $fname = $req_temp.'/'.$req_fun;  
  94. if(!emptyempty($req_fun)&&in_array($req_fun,static::$allow_sys_fun)){  
  95. $req_fun();  
  96. }else{  
  97. if(!emptyempty($req_fun)&&file_exists($p.$fname.'.php') ){  
  98. include$p.$fname.'.php' );  
  99. }else{  
  100. $fname = emptyempty($req_temp) ? 'index' : $req_temp;  
  101. if(file_exists($p.$fname.'.php') ){  
  102. include$p.$fname.'.php' );  
  103. }else{  
  104. $fname = $req_temp.'/index';  
  105. if(file_exists($p.$fname.'.php')){  
  106. include$p.$fname.'.php' );  
  107. }else{  
  108. //这个地方是对"个人主页"的这种特殊链接定向到"profile/"了,可自己修改  
  109. //如:www.xxx.com/12/将表示www.xxx.com/profile/?uid=12或www.xxx.com/profile?uid=12  
  110. $uid = is_numeric($req_temp) ? $req_temp : strstr($req_temp'/', true);  
  111. $ufun = is_numeric($req_temp) ? 'index' : strstr($req_temp'/');  
  112. if(is_numeric($uid)){  
  113. self::$uid = $uid;  
  114. if(!isset($_GET['uid'])) $_GET['uid'] = $uid;  
  115. $fname = 'profile/'.$ufun;  
  116. if(file_exists($p.$fname.'.php')){  
  117. include$p.$fname.'.php' );  
  118. }else{  
  119. header("location:".$w);  
  120. exit();  
  121. }  
  122. }else if(file_exists($p.'index.php')){  
  123. $fname = 'index';  
  124. include$p.'index.php' );  
  125. }else{  
  126. header("location:".$w);  
  127. exit();  
  128. }  
  129. }  
  130. }  
  131. }  
  132. $ev_fname = strrpos($fname,'/')===false ? $fname : substr($fname,strrpos($fname,'/')+1);  
  133. $ev_fname = static::$pretag.$ev_fname;  
  134. ifclass_exists($ev_fname, false) && method_exists($ev_fname,$req_fun)){  
  135. static::$linktag = $req_fun=='index' ? $fname.'/' : $fname.'/'.$req_fun;  
  136. if($req_fun != 'init' && method_exists($ev_fname,'init')){  
  137. $ev_fname::init();  
  138. }  
  139. $ev_fname::$req_fun();  
  140. }else ifclass_exists($ev_fname, false) && method_exists($ev_fname,'index') ){  
  141. static::$linktag = $fname.'/';  
  142. if(method_exists($ev_fname,'init')){  
  143. $ev_fname::init();  
  144. }  
  145. $ev_fname::index();  
  146. }else if$fname != 'index' && class_exists(static::$pretag.'index', false) && method_exists(static::$pretag.'index','index') ){  
  147. $ev_fname = static::$pretag.'index';  
  148. static::$linktag = 'index/';  
  149. if(method_exists($ev_fname,'init')){  
  150. $ev_fname::init();  
  151. }  
  152. $ev_fname::index();  
  153. }else{  
  154. self::__msg('Function Not Exist!');  
  155. }  
  156. }  
  157. }  
  158. self::__end();  
  159. }  
  160. //这里是用户自定义链接的解析(用数据库教程存储的解析值) 如: xiaoming.baidu.com  
  161. //数据库中 xiaoming这个标签指向一个人的博客 就会到了www.baidu.com/blog?uid=12或www.baidu.com/blog?uname=xiaoming(这个就看自己咋设计数据库了)  
  162. public static function __goto($para = '',$path = ''){  
  163. $w = static::$website_path;  
  164. if(emptyempty($para)){  
  165. exit('未知链接,解析失败,不能访问');  
  166. }  
  167. if(class_exists('Parseurl')){  
  168. $prs = Parseurl::selectone(array('tag','=',$para));  
  169. self::__msg($prs);  
  170. if(!emptyempty($prs)){  
  171. $parastr = $prs['tag'];  
  172. $output = array();  
  173. $_GET[$prs['idtag']] = $prs['id'];  
  174. parse_str($prs['parastr'], $output);  
  175. $_GET = array_merge($_GET,$output);  
  176. $path = $prs['type'].'/'.preg_replace('|^/'.$prs['type'].'|','',$path);  
  177. self::__msg($path);  
  178. header('location:'.$w.$path.'?'.http_build_query($_GET));  
  179. exit();  
  180. }else{  
  181. header("location:".$w);  
  182. exit();  
  183. }  
  184. }else{  
  185. header("location:".$w);  
  186. exit();  
  187. }  
  188. }  
  189. }  
  190. ?> 

推荐阅读
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
author-avatar
手机用户2502906281
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有