1. General database connection class
Important interfaces:
The interface is used to store MySQL connection data. Classes that implement this interface can use this data.
A simple and necessary part of the program can be isolated through the interface, and any program can implement this interface.
Interfaces are defined through interfaces and implemented through 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>?>
Universal MySQL connection classes and static variables:
Interface implementations connect access values through domain resolution operators. Using private static variable reception can take advantage of the speed of static variable processing and ensure encapsulation.
Try to avoid using global variables, as global variables will destroy encapsulation. Static variables help reduce instantiation of classes.
<?<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>?>
Implementing simple connection operations through a class and interface can greatly reduce development time, modifications are easy, and all information is stored in constants.
Agent mode:
The protection agent will only send the request to the real topic after verifying the request. This real subject is the target of the request, such as accessing database information.
Personal understanding: The user sends a request, and after the proxy module receives the request, it goes to the verification module. If it is correct, it returns a true value, and the proxy module then directs to the target of the real request.
Complete database connection and database selection through common classes.
General classes include: interfaces containing database information and classes that implement database connections.
The protection agent includes: interface, agent module that implements verification, and implemented client object class.
Process: Login page——>Client.php (implemented client object class)——>Proxy.php (proxy module)——>Use the general class to connect to the database for judgment, and if it is correct, return the proxy true value—— —>The agent directs the page to realProject.php for processing.
The role of the proxy is to ensure that only authorized users can access the website.
The proxy participant in the proxy mode can be regarded as a place where some operations that truly ensure high security can be done before the user accesses the real topic.
The proxy module is a simple password check that can call a high-security module to handle sensitive information.
Interface file
<?<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>?>
Agent class
<?<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 class that receives information sent by the client and sends the message to the proxy class for verification.
<?<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>?>
The real theme can only be entered by the proxy class.
<?<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>?>