-
- #------------- .htaccess start ---------------
- RewriteEngine on
- RewriteRule !.(js|ico| gif |jpg|png|css|swf|htm|txt)$index.php
- php_flagmagic_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 (!define('SITE_PASS')){
- echo self::$linktag.'
//} - }
//「http:」にアクセスしたときに実行されます。 //localhost/testpk/"
- public static functionindex(){
- echo 'test';
- }
//「http://localhost/testpk/blank」にアクセスすると、 「http://localhost/testpk/index/blank」として実行または記述されます通常「index/」は省略可能
- public static functionblank(){}
- }
- ?>
-
コードをコピー
class.rewrite.php;
-
-
- class Rewrite{
- public static $debug = false;//デバッグをオンにするかどうか
- public static $time_pass = 0;// module file
- 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') などのシステム関数が許可されている場合、システムは http://localhost/phpinfo または http の場合にリンクが phpinfo コンテンツにアクセスすることを許可します。 ://localhost/.... ../phpinfo の場合、phpinfo 関数は phpinfo.php モジュール ファイルを使用せずに直接実行されます。
- private static $allow_sys_fun = array();
プライベート静的関数 __get_microtime(){
- list( $usec, $sec) =explode(" ",microtime());
- return ((float)$usec + (float)$sec);
- }
//デバッグの設定 書き換え ::__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;
- }
//デバッグ関数
- パブリック静的関数 __msg($str){
- if(static::$debug){
- echo "n
n".print_r($str,true)."n n";
- }
- }
//解析開始時間
- パブリック静的関数__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');
- }
- }
//内部クロスモジュールtest1.php モジュール ページなどの URL 解析呼び出し Rwrite::__parseurl('/test2/show') という文が実行されると、test2.php モジュール ページの show メソッド (Rw_test2 クラスのメソッド) が
- パブリック静的関数 __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 = strto lower(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) ? 'インデックス' : $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{< /p>
//これは、「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 = ' インデックス/';
- if(method_exists($ev_fname,'init')){
- $ev_fname::init();
- }
- $ev_fname::index();
- }else{
- self::__msg('関数ではありませんExist!');
- }
- }
- }
- self::__end();
- }
//ここでは、ユーザー定義リンクの解析を示します (データベース) 例: xiaoming.baidu.com
- //データベース内の xiaoming タグが人のブログを指している場合、www.baidu.com/blog?uid=12 または www.baidu.com/blog? に移動します。 uname=xiaoming (データベースの設計方法を参照)
- パブリック静的関数 __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();
- }
- }
- }
- ?>
-
-
コードをコピー
|