欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

專為新手寫的結(jié)合smarty的類第1/3頁

 更新時間:2006年12月02日 00:00:00   作者:  

專為新手寫的結(jié)合smarty的類,誠摯邀請大家多提寶貴意見


這個是為新手(也為自己)寫的結(jié)合smarty一起使用的類,是未完成的,現(xiàn)在放出來的目的不是馬上讓新手使用,所以也沒有把注解寫的非常詳細(xì)
希望各位高手多多提意見,我盡量完善它。
    首先聲明,我寫這個的目的也是為了鍛煉自己,盡管我知道現(xiàn)在已經(jīng)有很多類似的類了,但是我還是決定來寫一個,
所以請大家在瀏覽的時候口下留情。

還有文件我也已經(jīng)打包上傳了,放在下面,請大家多多下載,多多提意見。有什么問題直接Q我

目前該類包括以下功能(使用范例,設(shè)$m = new Machine_m())
[數(shù)據(jù)庫]
目前支持MYSQL與ACCESS兩種數(shù)據(jù)庫
配置參考config.php文件
使用:$m->send_query(SQL語句)//
$m->select_query(SQL語句,是否返回資源,默認(rèn)為false即返回一個二維數(shù)組)

[錯誤處理]
分為系統(tǒng)錯誤和用戶錯誤
系統(tǒng)錯誤:
$this->sys_err( '配置錯誤,請檢查config配置文件', 'die');
第一個參數(shù)將記錄錯誤信息到/lib/error/system.err中,第二個參數(shù)是處理方法(keep or die),如果需要修改瀏覽器端提示的話,還可以設(shè)置第三個參數(shù),它默認(rèn)為“很抱歉,本站發(fā)生系統(tǒng)錯誤,請稍候再試?!?
用戶錯誤:
$m->user_err( '注冊系統(tǒng)關(guān)閉', 'die', $_SERVER['HTTP_REFERER'] );
第一個參數(shù)是顯示到瀏覽器端的提示,第二個參數(shù)是處理方法(keep or die),第三個參數(shù)是跳轉(zhuǎn)頁面,如果需要記錄錯誤信息的話,還可以設(shè)置第四個參數(shù),它將記錄錯誤信息
到/lib/error/user.err中,不設(shè)默認(rèn)不保存。

瀏覽器端的錯誤提示默認(rèn)調(diào)用/lib/error/下的err_page.htm模版文件,也可以設(shè)置自己的錯誤模版文件,然后用$m->err_page=加載。

[靜態(tài)生成]
只用短短一行就自動生成靜態(tài)頁面,跳轉(zhuǎn)時可設(shè)置靜態(tài)頁面過期時間
(現(xiàn)在還是不完全的靜態(tài),完全的比較復(fù)雜,目前沒有整合,大家如果要實(shí)現(xiàn)完全靜態(tài)的話,可以結(jié)合我的create_html函數(shù)和文本操作系列函數(shù)實(shí)現(xiàn))
使用方法:
$m->create_html(模版文件,靜態(tài)輸出路徑,輸出的文件名);
跳轉(zhuǎn):
$m->goto_html();
輸出文件名默認(rèn)等于當(dāng)前php文件的文件名,提供這個參數(shù)目的是,當(dāng)有需要使用靜態(tài)分頁時,可以用這個參數(shù)設(shè)置

[二維數(shù)組排序 (推薦)]
可以讓二維數(shù)組作類似:“先按字段a升序,再按字段b降序”這樣的排序
使用方法:
設(shè)有這樣一個數(shù)組:$x = array( array('name'=>'machine_馬', 'age'=>23),array('name'=>'tom',age=>28),…… )
我們現(xiàn)在要把此此數(shù)組先按name升序排,再按age降序排
則用法為m_sort($x,'name',SORT_ASC,'age',SORT_DESC)

[動態(tài)加載]
對于不常用的功能,我采用加載的方法來使用,個人認(rèn)為這樣可以節(jié)約資源
例如,如果我們要使用m_sort函數(shù)時,默認(rèn)是沒有加載這個函數(shù)的
需要這樣加載:$m->load_func('m_sort')
然后就可以使用m_sort函數(shù)了

[分頁]
這里我也不知道做得好不好,我是先寫了一個類,再寫一個函數(shù)來調(diào)它,目的是使用得時候比較方便
使用方法:m_page(數(shù)據(jù)條數(shù),當(dāng)前頁號,每頁多少行,顯示多少個跳轉(zhuǎn)鏈接)
函數(shù)返回一個數(shù)組:array(
  'rows' => 每頁顯示多少行,
  'prve' => 上一大頁頁號,//所謂大頁,就是類似上7頁,下7頁這樣的跳轉(zhuǎn)
  'next' => 下一大頁頁號,
  'pages' => 共有多少頁,
  'start' => SQL查詢的開始記錄數(shù),
  'count' => 共有多少條記錄,
  'links' => 鏈接頁號,//如果共有13頁,鏈接數(shù)是7個,當(dāng)前又在第二大頁,則輸出array(8,9,10,11,12,13)
  'current_page' => 當(dāng)前頁號
  );

[驗證表單]
把需要驗證的表單事先寫進(jìn)函數(shù)類,判別的時候只需要把$_POST傳入就可以了
使用方法:這個大家自己看函數(shù)就應(yīng)該看得明白,這個函數(shù)需要大家按照自己的需求來修改的

[防止跨站攻擊]
把這個功能也寫道一個函數(shù)內(nèi)了

[中文截取函數(shù)]
不是我寫的,拿來修改了一下而已

[上傳文件]
m_up_file($_FILES,上傳路徑,文件類型,大小限制)
其中上傳路徑可以這樣設(shè)置,1:直接寫文件夾路徑,2:array('gif'=>'file/gif','jpg'=>'file=>jpg'),這樣gif文件自動放入file/gif文件夾,jpg文件放入file/jpg文件夾
文件類型:寫法1:'jpg',寫法2:array('jpg','jpeg','gif')
返回array( 'arr' => 已上傳的文件數(shù)組, 'err_msg' => 上傳過程中的錯誤信息, 'num' => 上傳成功數(shù) )

[文本操作 (推薦)]
假設(shè)有這樣一個字符串 $str="你好<!--content-->phpchina<!--/content-->";
我們可以這樣修改 $new_str=m_txt_replace('content','machine_馬',$str);
現(xiàn)在$new_str的值為"你好<!--content-->machine_馬<!--/content-->"
其他的幾個函數(shù),如:m_txt_add,m_txt_delete,m_txt_get都是類似的,大家可以自行參看。

注意:這個就是生成靜態(tài)頁面之后,修改的方法。
可以參見6to23并思考為什么他一篇帖子放了那么多回帖速度還那么快
答:因為它的回帖沒有進(jìn)數(shù)據(jù)庫而是直接寫到靜態(tài)文件里面,再使用類似我上面的方法來修改的。你可以看他的源代碼,找找<!--你就知道了。


目前已經(jīng)基本實(shí)現(xiàn)就是以上功能了
接下來還打算做

[圖片處理]
[UBB代碼輸出]

等一些其他常用功能

希望各位高手多多提意見
主類:

復(fù)制代碼 代碼如下:


<?php 
session_cache_limiter( 'private,must-revalidate'); 
session_start(); 

if ( !defined( 'THIS_DIR' ) ) 

    die( '系統(tǒng)錯誤:路徑常量丟失' ); 

if ( @!include_once( THIS_DIR . 'lib/smarty/Smarty.class.php' ) ) 

    die( '系統(tǒng)錯誤:smarty文件丟失' ); 


unset( $GLOBALS ); 
unset( $HTTP_COOKIE_VARS ); 
unset( $HTTP_ENV_VARS ); 
unset( $HTTP_GET_VARS ); 
unset( $HTTP_POST_FILES ); 
unset( $HTTP_POST_VARS ); 
unset( $HTTP_SERVER_VARS ); 
unset( $HTTP_SESSION_VARS ); 

class Machine_m extends Smarty 

    // 數(shù)據(jù)庫資源 
    private $conn; 
    // 路徑 
    private $the_dir; 
    // 配置文件 
    private $config = array(); 
    private $config_url; 
    // 外部函數(shù)列表 
    private $func_list = array(); 
    private $func_list_url; 
    // 錯誤提示頁面 
    public $err_page = 'lib/error/err_page.htm'; 
    // 靜態(tài)生成 
    public $html_cache = 'html'; 
    public $html_cache_lifetime = 86400; 

    // 構(gòu)造函數(shù) 
    public function __construct( $test = false ) 
    { 
        // 保留smarty類的構(gòu)造部分 
        $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] 
        : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']); 

        // 現(xiàn)在是machine_m的構(gòu)造部分 
        $this->left_delimiter = '<%'; 
        $this->right_delimiter = '%>'; 
        $this->the_dir = THIS_DIR; 
        $this->config_url = "{$this->the_dir}lib/config/config.php"; 
        $this->config = $this->parse_ini(); 
        $this->func_list_url = "{$this->the_dir}lib/config/func_list.php"; 
        $this->func_list = $this->parse_func(); 
        $this->state( $test ); 
        $this->load_func( array( 'm_addslashes', 'm_stripslashes' ) ); 
        $this->connect(); 
    } 

    // 析構(gòu)函數(shù) 
    public function __destruct() 
    { 
    } 

    // 設(shè)置網(wǎng)站狀態(tài)函數(shù) 
    private function state( $test ) 
    { 
        if ( $test == true) 
        { 
            $this->load_func( array( 'm_print_r', 'm_var_dump' ) ); 
            $this->compile_check = true; 
            error_reporting(E_ALL); 
        } 
        else 
        { 
            $this->compile_check = false; 
            error_reporting(0); 
        } 
    } 

    // 解析配置文件函數(shù) 
    private function parse_ini() 
    { 
        if ( !file_exists( $this->config_url ) ) 
        { 
            $this->sys_err( "config配置文件丟失", 'die' ); 
        } 
        $config = parse_ini_file( $this->config_url ); 
        if ( !array_key_exists( 'host_name', $config ) 
        || !array_key_exists( 'user_name', $config ) 
        || !array_key_exists( 'db_name', $config ) 
        || !array_key_exists( 'password', $config ) ) 
        { 
            $this->sys_err( '配置錯誤,請檢查config配置文件', 'die'); 
        } 
        $config = $this->decode_config( $config ); 
        settype( $config, 'object'); 
        return $config; 
    } 

    // 解析函數(shù)列表函數(shù) 
    private function parse_func() 
    { 
        if ( !file_exists( $this->func_list_url ) ) 
        { 
            $this->sys_err( "func_list配置文件丟失", 'die' ); 
        } 
        $func_list = parse_ini_file( $this->func_list_url ); 
        return $func_list; 
    } 

    // 外部函數(shù)加載函數(shù) 
    public function load_func( $func ) 
    { 
        if ( is_array( $func ) ) 
        { 
            foreach ( $func as $func_name ) 
            { 
                $this->include_file( $this->func_list[$func_name] ); 
            } 
        } 
        else 
        { 
            $this->include_file( $this->func_list[$func] ); 
        } 
    } 

    // 外部函數(shù)包含函數(shù) 
    public function include_file( $file_url ) 
    { 
        $file_url = $this->the_dir .$file_url; 
        @$ok = include_once( $file_url ); 
        if ( $ok != true ) 
        { 
            $this->sys_err( "文件{{$file_url}}加載失敗", 'die' ); 
        } 
    } 

    // 對config文件解碼函數(shù)(將數(shù)據(jù)庫用戶名和密碼明文紀(jì)錄是不安全的,最好先加密,再在此解密,本函數(shù)可以重載) 
    protected function decode_config( $config ) 
    { 
        return $config; 
    } 

    // 連接數(shù)據(jù)庫函數(shù) 
    private function connect() 
    { 
        switch ( strtoupper( $this->config->database ) ) 
        { 
            case 'MYSQL' : 
                $this->connect_mysql(); 
                break; 
            case 'ACCESS' : 
                $this->connect_access(); 
                break; 
            default : 
                $this->sys_err( '數(shù)據(jù)庫類型錯誤,該類目前只支持MYSQL與ACCESS兩種數(shù)據(jù)庫', 'die'); 
                break; 
        } 
    } 

    // 連接MYSQL數(shù)據(jù)庫函數(shù) 
    private function connect_mysql() 
    { 
        if ( $this->conn != null ) 
        { 
            @mysql_close( $this->conn ); 
            $this->conn = null; 
        } 
        @$this->conn = mysql_connect( $this->config->host_name, $this->config->user_name, $this->config->password ); 
        if ( $this->conn == false ) 
        { 
            $mysql_err = mysql_error(); 
            $this->sys_err( "MYSQL數(shù)據(jù)庫連接失敗,原因是:{{$mysql_err}}", 'die' ); 
        } 
        @$db = mysql_select_db( $this->config->db_name, $this->conn ) ; 
        if ( $db == false ) 
        { 
            $mysql_err = mysql_error(); 
            $this->sys_err( "數(shù)據(jù)表連接失敗,原因是:{{$mysql_err}}", 'die' ); 
        } 
    } 

    // 連接ACCESS數(shù)據(jù)庫函數(shù) 
    private function connect_access() 
    { 
        if ( $this->conn != null ) 
        { 
            @odbc_close( $this->conn ); 
            $this->conn = null; 
        } 
        $dsn = 'Driver=Microsoft Access Driver (*.mdb);dbq=' . realpath( $this->the_dir . $this->config->db_name ); 
        @$this->conn = odbc_connect( $dsn, $this->config->user_name, $this->config->password ); 
        if ( $this->conn == false ) 
        { 
            @$odbc_err = odbc_errormsg( $this->conn ); 
            $this->sys_err( "ACCESS數(shù)據(jù)庫連接失敗,原因是:{{$odbc_err}}", 'die' ); 
        } 
    }  

相關(guān)文章

最新評論