一、數(shù)據(jù)庫連接通用類
重要的接口:
接口用來存儲MySQL連接數(shù)據(jù)。實(shí)現(xiàn)這個(gè)接口的類都可以使用這些數(shù)據(jù)。
通過接口可以隔離出程序中一個(gè)簡單而必要的部分,任何程序都可以實(shí)現(xiàn)這個(gè)接口。
接口通過interface來定義,通過implements實(shí)現(xiàn)。
<?php //文件名IConnectInfo.php interface IConnectInfo { const Host = "localhost"; const UserName = "root"; const Password = ""; const DBName = "bergift"; public function doConnect(); } ?>
通用MySQL連接類和靜態(tài)變量:
接口實(shí)現(xiàn)都通過域解析操作符來連接訪問值。使用私有靜態(tài)變量接收,可以使用靜態(tài)變量處理的速度優(yōu)勢,還可以保證封裝性。
盡量避免使用全局變量,全局變量會破壞封裝。靜態(tài)變量有助于減少類的實(shí)例化。
<?php include_once('IConnectInfo.php'); class UniversalConnect implements IConnectInfo { private static $Server = IConnectInfo::Host; private static $CurrentDB = IConnectInfo::DBName; private static $User = IConnectInfo::UserName; private static $Password = IConnectInfo::Password; private static $HookUp; public function doConnect(){ self::$HookUp = mysqli_connect(self::$Server,self::$User,self::$Password,self::$CurrentDB); if(self::$HookUp){ }elseif(mysqli_connect_error(self::$HookUp)){ echo "Fail: ".mysqli_connect_error; } return self::$HookUp; } } ?>
通過一個(gè)類和接口實(shí)現(xiàn)簡單的連接操作,可大大減少開發(fā)的時(shí)間,修改很容易,所有信息都存儲在常量中。
代理模式:
保護(hù)代理在驗(yàn)證過請求之后,才會把請求發(fā)送到真實(shí)主題。這個(gè)真實(shí)主題就是請求的目標(biāo),如訪問數(shù)據(jù)庫信息。
個(gè)人理解:用戶發(fā)送請求,代理模塊接收到請求之后,轉(zhuǎn)到驗(yàn)證模塊,正確則返回真值,代理模塊再導(dǎo)向真正請求的目標(biāo)。
通過通用類完成數(shù)據(jù)庫的連接,數(shù)據(jù)庫的選擇。
通用類包括:包含數(shù)據(jù)庫信息的接口、實(shí)現(xiàn)數(shù)據(jù)庫連接的類。
保護(hù)代理包括:接口、實(shí)現(xiàn)校驗(yàn)的代理模塊、實(shí)現(xiàn)的客戶端對象類。
流程:登錄頁面——>Client.php(實(shí)現(xiàn)的客戶端對象類)——>Proxy.php(代理模塊)——>使用通用類連接數(shù)據(jù)庫進(jìn)行判斷,正確則返回代理真值——>代理將頁面導(dǎo)向至realProject.php進(jìn)行處理。
代理的作用是確保有權(quán)限的用戶才能訪問網(wǎng)站。
可以把代理模式中代理參與者看做是一個(gè)場所,在用戶訪問真實(shí)主題前可以在這里做一些真正確保高安全性的操作。
代理模塊是一個(gè)簡單的口令檢查,可以調(diào)用一個(gè)高安全性模塊來處理敏感信息。
接口文件
<?php //文件名ISubject.php Interface ISubject { function request(); } ?>
代理類
<?php //文件名Proxy.php include_once('ISubject.php'); include_once('RealSubject.php'); include_once('UniversalConnect.php'); class Proxy implements ISubject { private $TableMaster; private $HookUp; private $LoginSuccess; private $RealSubject; public function login($UserNow,$PassNow) { $UserName = $UserNow; $PassWord = md5($PassNow); $this->LoginSuccess = false; $this->TableMaster = "BAdmin"; $this->HookUp = UniversalConnect::doConnect(); $sql = "SELECT password from $this->TableMaster WHERE username = '$UserName'"; if($result = $this->HookUp->query($sql)) { $row = $result->fetch_array(MYSQLI_ASSOC); if($row['password']==$PassWord) { $this->LoginSuccess = true; } $result->close(); } elseif(($result = $this->HookUp->query($sql))===false) { echo "Failed".$this->HookUp->error; exit(); } $this->HookUp->close(); if($this->LoginSuccess) { $this->request(); } else { header("Location:index.php"); } } public function register($UserNow,$PassNow) { $UserName = $UserNow; $PassWord = md5($PassNow); $this->LoginSuccess = false; $this->TableMaster = "BAdmin"; $this->HookUp = UniversalConnect::doConnect(); $sql = "INSERT INTO $this->TableMaster VALUES('$UserName','$PassWord')"; if($result = $this->HookUp->query($sql)) { $this->LoginSuccess = true; } elseif(($result = $this->HookUp->query($sql))===false) { echo "Failed".$this->HookUp->error; exit(); //header("Location:index.php"); } $this->HookUp->close(); if($this->LoginSuccess) { echo "<script>alert('Success!');</script>"; } else { header("Location:index.php"); } } public function request() { $this->realSubject = new RealSubject(); $this->realSubject->request(); } } ?>
接收客戶端發(fā)送的信息的Client類,發(fā)送消息到代理類進(jìn)行驗(yàn)證。
<?php //文件名Client.php include_once('Proxy.php'); class Client { private $Proxy; private $UserName; private $PassWord; public function __construct() { $this->TableMaster = 'BAdmin'; $this->HookUp = UniversalConnect::doConnect(); $this->UserName = $this->HookUp->real_escape_string(trim($_POST['username'])); $this->PassWord = $this->HookUp->real_escape_string(trim($_POST['password'])); if($_GET['do']==='register') { $this->getRegis($this->proxy=new Proxy()); }elseif($_GET['do']==='login') { $this->getIface($this->proxy=new Proxy()); } } private function getIface(ISubject $proxy) { $proxy->login($this->UserName,$this->PassWord); } private function getRegis(ISubject $proxy) { $proxy->register($this->UserName,$this->PassWord); } } $Worker = new Client(); ?>
真實(shí)主題,只能由代理類進(jìn)入。
<?php //文件名RealSubject.php include_once('ISubject.php'); class RealSubject implements ISubject { public function request() { session_save_path(dirname(__FILE__).'/sess/'); @session_start(); $_SESSION['bergift'] = 'admin'; header("Location:main.php"); } } ?>

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)