首頁 > php教程 > php手册 > PHP與MySQL設計模式:代理模式

PHP與MySQL設計模式:代理模式

WBOY
發布: 2016-09-20 03:30:25
原創
1363 人瀏覽過

  一、資料庫連接通用類別

  重要的介面:

  介面用來儲存MySQL連接資料。實作這個介面的類別都可以使用這些資料。

  透過介面可以隔離出程式中一個簡單而必要的部分,任何程式都可以實作這個介面。

  介面透過interface來定義,透過implements實現。

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">文件名IConnectInfo.php</span>
<span style="color: #0000ff;">interface</span><span style="color: #000000;"> IConnectInfo
{
    </span><span style="color: #0000ff;">const</span> Host     = "localhost"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">const</span> UserName = "root"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">const</span> Password = ""<span style="color: #000000;">;
    </span><span style="color: #0000ff;">const</span> DBName   = "bergift"<span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doConnect();
}
</span>?>
登入後複製

  通用MySQL連接類別與靜態變數:

  介面實作都透過域解析運算子來連接存取值。使用私有靜態變數接收,可以使用靜態變數處理的速度優勢,還可以保證封裝性。

  盡量避免使用全域變量,全域變數會破壞封裝。靜態變數有助於減少類別的實例化。

<?<span style="color: #000000;">php
</span><span style="color: #0000ff;">include_once</span>('IConnectInfo.php'<span style="color: #000000;">);
</span><span style="color: #0000ff;">class</span> UniversalConnect <span style="color: #0000ff;">implements</span><span style="color: #000000;"> IConnectInfo
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$Server</span>    = IConnectInfo::<span style="color: #000000;">Host;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$CurrentDB</span> = IConnectInfo::<span style="color: #000000;">DBName;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$User</span>      = IConnectInfo::<span style="color: #000000;">UserName;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$Password</span>  = IConnectInfo::<span style="color: #000000;">Password;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$HookUp</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doConnect(){
        self</span>::<span style="color: #800080;">$HookUp</span> = <span style="color: #008080;">mysqli_connect</span>(self::<span style="color: #800080;">$Server</span>,self::<span style="color: #800080;">$User</span>,self::<span style="color: #800080;">$Password</span>,self::<span style="color: #800080;">$CurrentDB</span><span style="color: #000000;">);
        
        </span><span style="color: #0000ff;">if</span>(self::<span style="color: #800080;">$HookUp</span><span style="color: #000000;">){
            
        }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">mysqli_connect_error</span>(self::<span style="color: #800080;">$HookUp</span><span style="color: #000000;">)){
            </span><span style="color: #0000ff;">echo</span> "Fail: ".<span style="color: #008080;">mysqli_connect_error</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$HookUp</span><span style="color: #000000;">;
    }
}
</span>?>
登入後複製

  透過一個類別和介面實現簡單的連接操作,可大幅減少開發的時間,修改很容易,所有資訊都儲存在常數中。

  代理模式:

  保護代理在驗證過請求之後,才會把請求送到真實主題。這個真實主題就是請求的目標,例如存取資料庫資訊。

  個人理解:用戶發送請求,代理模組接收到請求之後,轉到驗證模組,正確則返回真值,代理模組再導向真正請求的目標。

  透過通用類別完成資料庫的連接,資料庫的選擇。

  通用類別包括:包含資料庫資訊的介面、實作資料庫連線的類別。

  保護代理程式包括:介面、實作校驗的代理模組、實作的客戶端物件類別。

  流程:登入頁面—>Client.php(實作的客戶端物件類別)—>Proxy.php(代理模組)—>使用通用類別連接資料庫進行判斷,正確則傳回代理真值—>代理將頁面導向至realProject.php進行處理。

  代理的作用是確保有權限的使用者才能存取網站。

  可以把代理模式中代理參與者看做是一個場所,在用戶訪問真實主題之前可以在這裡做一些真正確保高安全性的操作。

  代理模組是一個簡單的口令檢查,可以呼叫一個高安全性模組來處理敏感資訊。

  介面檔

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">文件名ISubject.php</span>
<span style="color: #0000ff;">Interface</span><span style="color: #000000;"> ISubject
{
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> request();
}
</span>?>
登入後複製

  代理類

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">文件名Proxy.php</span>
<span style="color: #0000ff;">include_once</span>('ISubject.php'<span style="color: #000000;">);
</span><span style="color: #0000ff;">include_once</span>('RealSubject.php'<span style="color: #000000;">);
</span><span style="color: #0000ff;">include_once</span>('UniversalConnect.php'<span style="color: #000000;">);

</span><span style="color: #0000ff;">class</span> Proxy <span style="color: #0000ff;">implements</span><span style="color: #000000;"> ISubject
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$TableMaster</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$HookUp</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$LoginSuccess</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$RealSubject</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> login(<span style="color: #800080;">$UserNow</span>,<span style="color: #800080;">$PassNow</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$UserName</span> = <span style="color: #800080;">$UserNow</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$PassWord</span> = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$PassNow</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>->LoginSuccess = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->TableMaster  = "BAdmin"<span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->HookUp        = UniversalConnect::<span style="color: #000000;">doConnect();
        
        </span><span style="color: #800080;">$sql</span> = "SELECT password from <span style="color: #800080;">$this</span>->TableMaster WHERE username = '<span style="color: #800080;">$UserName</span>'"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->HookUp->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">))
        {
            </span><span style="color: #800080;">$row</span> = <span style="color: #800080;">$result</span>-><span style="color: #000000;">fetch_array(MYSQLI_ASSOC);
            </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$row</span>['password']==<span style="color: #800080;">$PassWord</span><span style="color: #000000;">)
            {
                </span><span style="color: #800080;">$this</span>->LoginSuccess = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
            }
            </span><span style="color: #800080;">$result</span>-><span style="color: #000000;">close();
        }
        </span><span style="color: #0000ff;">elseif</span>((<span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->HookUp->query(<span style="color: #800080;">$sql</span>))===<span style="color: #0000ff;">false</span><span style="color: #000000;">)
        {
            </span><span style="color: #0000ff;">echo</span> "Failed".<span style="color: #800080;">$this</span>->HookUp-><span style="color: #000000;">error;
            </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">();
        }
        </span><span style="color: #800080;">$this</span>->HookUp-><span style="color: #000000;">close();
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">LoginSuccess)
        {
            </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">request();
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            </span><span style="color: #008080;">header</span>("Location:index.php"<span style="color: #000000;">);
        }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> register(<span style="color: #800080;">$UserNow</span>,<span style="color: #800080;">$PassNow</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$UserName</span> = <span style="color: #800080;">$UserNow</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$PassWord</span> = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$PassNow</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>->LoginSuccess = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->TableMaster  = "BAdmin"<span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->HookUp        = UniversalConnect::<span style="color: #000000;">doConnect();
        
        </span><span style="color: #800080;">$sql</span> = "INSERT INTO <span style="color: #800080;">$this</span>->TableMaster VALUES('<span style="color: #800080;">$UserName</span>','<span style="color: #800080;">$PassWord</span>')"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->HookUp->query(<span style="color: #800080;">$sql</span><span style="color: #000000;">))
        {
            </span><span style="color: #800080;">$this</span>->LoginSuccess = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">elseif</span>((<span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->HookUp->query(<span style="color: #800080;">$sql</span>))===<span style="color: #0000ff;">false</span><span style="color: #000000;">)
        {
            </span><span style="color: #0000ff;">echo</span> "Failed".<span style="color: #800080;">$this</span>->HookUp-><span style="color: #000000;">error;
            </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">();
            </span><span style="color: #008000;">//</span><span style="color: #008000;">header("Location:index.php");</span>
<span style="color: #000000;">        }
        </span><span style="color: #800080;">$this</span>->HookUp-><span style="color: #000000;">close();
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">LoginSuccess)
        {
            </span><span style="color: #0000ff;">echo</span> "<script>alert('Success!');</script>"<span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            </span><span style="color: #008080;">header</span>("Location:index.php"<span style="color: #000000;">);
        }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> request()
    {
        </span><span style="color: #800080;">$this</span>->realSubject = <span style="color: #0000ff;">new</span><span style="color: #000000;"> RealSubject();
        </span><span style="color: #800080;">$this</span>->realSubject-><span style="color: #000000;">request();
    }
}
</span>?>
登入後複製

 

  接收客戶端發送的訊息的Client類,發送訊息到代理類別進行驗證。

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">文件名Client.php</span>
<span style="color: #0000ff;">include_once</span>('Proxy.php'<span style="color: #000000;">);
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Client
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$Proxy</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$UserName</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$PassWord</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct()
    {
        </span><span style="color: #800080;">$this</span>->TableMaster = 'BAdmin'<span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->HookUp      = UniversalConnect::<span style="color: #000000;">doConnect();
        </span><span style="color: #800080;">$this</span>->UserName    = <span style="color: #800080;">$this</span>->HookUp->real_escape_string(<span style="color: #008080;">trim</span>(<span style="color: #800080;">$_POST</span>['username'<span style="color: #000000;">]));
        </span><span style="color: #800080;">$this</span>->PassWord    = <span style="color: #800080;">$this</span>->HookUp->real_escape_string(<span style="color: #008080;">trim</span>(<span style="color: #800080;">$_POST</span>['password'<span style="color: #000000;">]));
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_GET</span>['do']==='register'<span style="color: #000000;">)
        {
            </span><span style="color: #800080;">$this</span>->getRegis(<span style="color: #800080;">$this</span>->proxy=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Proxy());
        }</span><span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$_GET</span>['do']==='login'<span style="color: #000000;">)
        {
            </span><span style="color: #800080;">$this</span>->getIface(<span style="color: #800080;">$this</span>->proxy=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Proxy());
        }
    }
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> getIface(ISubject <span style="color: #800080;">$proxy</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$proxy</span>->login(<span style="color: #800080;">$this</span>->UserName,<span style="color: #800080;">$this</span>-><span style="color: #000000;">PassWord);
    }
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> getRegis(ISubject <span style="color: #800080;">$proxy</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$proxy</span>->register(<span style="color: #800080;">$this</span>->UserName,<span style="color: #800080;">$this</span>-><span style="color: #000000;">PassWord);
    }
}
</span><span style="color: #800080;">$Worker</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Client();
</span>?>
登入後複製

  真實主題,只能由代理類別進入。

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;">文件名RealSubject.php</span>
<span style="color: #0000ff;">include_once</span>('ISubject.php'<span style="color: #000000;">);
</span><span style="color: #0000ff;">class</span> RealSubject <span style="color: #0000ff;">implements</span><span style="color: #000000;"> ISubject
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> request()
    {
        </span><span style="color: #008080;">session_save_path</span>(<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>).'/sess/'<span style="color: #000000;">);
        @</span><span style="color: #008080;">session_start</span><span style="color: #000000;">();
        </span><span style="color: #800080;">$_SESSION</span>['bergift'] = 'admin'<span style="color: #000000;">;
        </span><span style="color: #008080;">header</span>("Location:main.php"<span style="color: #000000;">);
    }
}
</span>?>
登入後複製

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板