PHP and MySQL design modes: proxy mode and mysql Design Mode
I. general database connection
Important interfaces:
Interface is used to store MySQL connection data. All classes that implement this interface can use this data.
You can isolate a simple and necessary part of a program through an interface. Any program can implement this interface.
Interfaces are defined by interfaces and implemented by implements.
<? Php // file name IConnectInfo. phpinterface IConnectInfo {const Host = "localhost"; const UserName = "root"; const Password = ""; const DBName = "bergift"; public function doConnect () ;}?>
Common MySQL connection classes and static variables:
All interface implementations connect the access value through the domain resolution operator. With Private static variable reception, you can use the speed advantage of static variable processing and ensure encapsulation.
Avoid using global variables as much as possible, which may damage encapsulation. Static variables help reduce class instantiation.
<?phpinclude_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; }}?>
A simple connection operation through a class and interface can greatly reduce the development time, make modifications easily, and store all the information in constants.
Proxy mode:
After the request is verified, the protection proxy sends the request to the real topic. The real topic is the request target, such as accessing database information.
Personal Understanding: the user sends a request. After receiving the request, the proxy module forwards the request to the verification module. If the request is correct, the proxy module returns the true value. The proxy module then directs the real request to the target.
You can use a common class to connect to a database and select a database.
Common classes include: interfaces that contain database information and classes that implement database connection.
Protection proxy includes interfaces, verification proxy modules, and client object classes.
Process: logon page --> Client. php (implemented client object class) --> Proxy. php (proxy module) --> use a common class to connect to the database for judgment, and return the true value of the proxy if it is correct --> the proxy directs the page to realProject. php.
Proxy is used to ensure that users with permissions can access the website.
Proxy participants can be viewed as a place in the proxy mode. Before users access the real theme, they can perform operations to ensure high security.
The proxy module is a simple password check that can call a high security module to process sensitive information.
Interface file
<? Php // file name: ISubject. phpInterface ISubject {function request () ;}?>
Proxy
<? Php // file name Proxy. phpinclude_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 = U NiversalConnect: 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-> clo Se (); 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-> quer Y ($ 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 () ;}}?>
The Client class that receives the information sent by the Client and sends the message to the proxy class for verification.
<? Php // file name Client. phpinclude_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 '] = 'reg Ister ') {$ 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 () ;?>
The actual topic can only be entered by the proxy class.
<? Php // file name RealSubject. phpinclude_once ('isubject. php '); class RealSubject implements ISubject {public function request () {session_save_path (dirname (_ FILE __). '/sess/'); @ session_start (); $ _ SESSION ['authorization'] = 'admin'; header ("Location: main. php ") ;}}?>