Rumah >php教程 >php手册 >PHP与MySQL设计模式:代理模式

PHP与MySQL设计模式:代理模式

WBOY
WBOYasal
2016-09-20 03:30:251350semak imbas

  一、数据库连接通用类

  重要的接口:

  接口用来存储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>?>

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:phpstorm 激活服务器Artikel seterusnya:PHP弱类型安全问题笔记