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

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

本代码是在apache上的LoadModulerewrite_modulemodulesmod_rewrite.so运行成功后的操作,利用php代码对网站url重定向为更复杂的逻辑结构
对于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

代码如下:


class Rw_testPk extends Rewrite {
//这个是前导函数,只要访问到testpk这个页面,这个必然会执行,可用来控制本页面内函数访问权限或本页面全局变量
public static function init(){
//if (!defined('SITE_PASS')){
echo self::$linktag.'
';//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;

代码如下:


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

\n".print_r($str,true)."\n
\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();
}
}
}
?>
推荐阅读
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
author-avatar
书友70030711
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有