PHP 高速 URL 書き換え更新版 [PHP 5.30 以降が必要]_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:38:38
オリジナル
781 人が閲覧しました

Apache の rewrite モジュールを開いて設定することは、この記事の主題ではありません。詳細な説明については、他の記事を参照してください。
このクラスは、以前のバージョン (別のクラス) から高速リダイレクトの特性を継承しています。 、すべて静的 Call を使用します)、他の URL のモジュールを呼び出すことができる非常に重要な関数と属性を追加します。これにより、モジュールとモジュール間、またはページ間での関数の共有が簡素化されます。

コードをコピーします。 コードは次のとおりです:
#------------- .htaccess start ---------------
RewriteEngine on
RewriteRule !。 (js|ico |gif|jpg|png|css|swf|htm|txt)$index.php
php_flag magic_quotes_gpc オフ
php_flag register_globals オフ
#------------- .htaccess end - --- -----------

書き換え機能の導入:サイトのルートディレクトリにあるindex.phpの最後に以下のコードを記述すると書き換えが有効になります(通常の状態: 1. Apache 書き換え構成が成功し、.htaccess サポートが有効になります。 2. サイトのルート ディレクトリに .htaccess ファイルが設定されます。 3. class.rewrite.php クラス ファイルが、index.php の先頭部分にロードされます。 4. ページ モジュール ファイルの場所と記述は正しいです) :

コードをコピーします コードは次のとおりです:
//....
書き換え:: __config(
$config['path'],/*'http:///xxxxx/mysite/'URL ベースの場所*/
$config['md_path'],/*'c:/phpsite/www/mysite/modules /'モジュールファイルの物理ディレクトリ*/
array(
'phpinfo'
)
);
Rewrite::__parse();
//.....

モジュールファイルの書き込み方法:
testPk.php

コードをコピーします コードは次のとおりです:
< ;?php
class Rw_testPk extends Rewrite {
//これは、testpk ページにアクセスしている限り、主要な関数です。このページ内の関数のアクセス許可、またはこのページのグローバル変数を制御するために使用できます
public static function init() {
//if (!define('SITE_PASS')){
echo self::$ linktag.'
';//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;//モジュールファイルの全体的な実行時間を取得する
public static $version = 2.2;
public static $pretag = 'Rw_';//モジュールファイルクラスの名前プレフィックス
public static $linktag = 'index';//ページリンクタグは、どのリンクが解析されるかをマークするために使用され、さまざまなメニュー効果とリンク アクセスの制御に使用されます 権限
protected static $time_start = 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://localhost/..../phpinfo が phpinfo を直接実行すると、システムはリンクから phpinfo コンテンツへのアクセスを許可します。関数、phpinfo.php モジュール ファイルは必要ありません
private static $allow_sys_fun = array();
private static function __get_microtime(){
list($usec, $sec) =explode(" ",microtime()); ((float)$usec + (float)$sec);
}
//デバッグを設定する Rewrite:: __debug(true);
static::$debug = $ d;
}
//設定パスと許可される関数
public static function __config($website_path = '',$physical_path = '',$allow_sys_fun = array()){
self::$physical_path = $physical_path; ::$website_path = $website_path;
self::$allow_sys_fun = $allow_sys_fun;
//デバッグ関数
public static function __msg($str){
if(static::$debug){
echo "n< pre>n".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(); :$time_end - self::$time_start),6) * 1000;
if($re){
return self::$time_pass;
}else{
self::__msg('PASS_TIME: '.self::$ time_pass.' ms');
}
}
//test1 のように、文 Rwrite::__parseurl('/test2/show') が実行されると、 test2.php モジュール ページの show メソッド (Rw_test2 クラスのメソッド) が呼び出されます。
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 = strto lower(basename( $p.$url , '.php' ));
static::$linktag = $part.$fun;
$fname = static::$pretag.$部分;
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 = '' ){
$p = static::$physical_path;
$req_execute = false ; $_SERVER[' REQUEST_URI'] : $Url;
$local = parse_url($url_p);
$req_path = preg_req('| [^w/.\]|','',$req ['パス']);
$req_para = empty($Url) ? strstr($_SERVER['SERVER_NAME'],'.',true) : 'www'; _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);
$ req_temp = implode('/',$req_path_rearr); = $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' ) ;
$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); ) ? 'インデックス' : strstr($req_temp, '/');
if(is_numeric($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);
}
}else if(file_exists($p.'index.php')){
$ fname = 'index';
}else("location:".$w);
}
}
}
$ 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(); ( 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 ::__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))
if(!empty($prs); )){
$parastr = $prs['tag'];
$output = array();
$_GET[$prs['idtag']] = $prs['id']; '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();
header("location:".$w);
}else{
exit(); }
}
}
?



http://www.bkjia.com/PHPjc/321736.html

www.bkjia.com

tru​​e

http://www.bkjia.com/PHPjc/321736.html

技術記事

Apache の書き換えモジュールを開いて設定することは、この記事の主題ではありません。詳細な説明については、他の記事を参照してください。このクラスは、PHP 5.30 以降のバージョンでのみ使用でき、以前のバージョンの高速リダイレクトを継承しています。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!