Home > 类库下载 > PHP类库 > PHP and MySQL design patterns: proxy pattern

PHP and MySQL design patterns: proxy pattern

高洛峰
Release: 2016-10-09 13:14:47
Original
1548 people have browsed it

一、数据库连接通用类

  重要的接口:

  接口用来存储MySQL连接数据。实现这个接口的类都可以使用这些数据。

  通过接口可以隔离出程序中一个简单而必要的部分,任何程序都可以实现这个接口。

  接口通过interface来定义,通过implements实现。

<?php
//文件名IConnectInfo.php
interface IConnectInfo
{
    const Host     = "localhost";
    const UserName = "root";
    const Password = "";
    const DBName   = "bergift";
    
    public function doConnect();
}
?>
Copy after login

通用MySQL连接类和静态变量:

  接口实现都通过域解析操作符来连接访问值。使用私有静态变量接收,可以使用静态变量处理的速度优势,还可以保证封装性。

  尽量避免使用全局变量,全局变量会破坏封装。静态变量有助于减少类的实例化。

<?php
include_once(&#39;IConnectInfo.php&#39;);
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;
    }
}
?>
Copy after login

通过一个类和接口实现简单的连接操作,可大大减少开发的时间,修改很容易,所有信息都存储在常量中。

  代理模式:

  保护代理在验证过请求之后,才会把请求发送到真实主题。这个真实主题就是请求的目标,如访问数据库信息。

  个人理解:用户发送请求,代理模块接收到请求之后,转到验证模块,正确则返回真值,代理模块再导向真正请求的目标。

  通过通用类完成数据库的连接,数据库的选择。

  通用类包括:包含数据库信息的接口、实现数据库连接的类。

  保护代理包括:接口、实现校验的代理模块、实现的客户端对象类。

  流程:登录页面——>Client.php(实现的客户端对象类)——>Proxy.php(代理模块)——>使用通用类连接数据库进行判断,正确则返回代理真值——>代理将页面导向至realProject.php进行处理。

  代理的作用是确保有权限的用户才能访问网站。

  可以把代理模式中代理参与者看做是一个场所,在用户访问真实主题前可以在这里做一些真正确保高安全性的操作。

  代理模块是一个简单的口令检查,可以调用一个高安全性模块来处理敏感信息。

  接口文件

<?php
//文件名ISubject.php
Interface ISubject
{
    function request();
}
?>
Copy after login

代理类

<?php
//文件名Proxy.php
include_once(&#39;ISubject.php&#39;);
include_once(&#39;RealSubject.php&#39;);
include_once(&#39;UniversalConnect.php&#39;);

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 = &#39;$UserName&#39;";
        if($result = $this->HookUp->query($sql))
        {
            $row = $result->fetch_array(MYSQLI_ASSOC);
            if($row[&#39;password&#39;]==$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(&#39;$UserName&#39;,&#39;$PassWord&#39;)";
        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(&#39;Success!&#39;);</script>";
        }
        else
        {
            header("Location:index.php");
        }
    }
    public function request()
    {
        $this->realSubject = new RealSubject();
        $this->realSubject->request();
    }
}
?>
Copy after login

接收客户端发送的信息的Client类,发送消息到代理类进行验证。

<?php
//文件名Client.php
include_once(&#39;Proxy.php&#39;);
class Client
{
    private $Proxy;
    private $UserName;
    private $PassWord;
    
    public function __construct()
    {
        $this->TableMaster = &#39;BAdmin&#39;;
        $this->HookUp      = UniversalConnect::doConnect();
        $this->UserName    = $this->HookUp->real_escape_string(trim($_POST[&#39;username&#39;]));
        $this->PassWord    = $this->HookUp->real_escape_string(trim($_POST[&#39;password&#39;]));
        if($_GET[&#39;do&#39;]===&#39;register&#39;)
        {
            $this->getRegis($this->proxy=new Proxy());
        }elseif($_GET[&#39;do&#39;]===&#39;login&#39;)
        {
            $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();
?>
Copy after login

 真实主题,只能由代理类进入。

<?php
//文件名RealSubject.php
include_once(&#39;ISubject.php&#39;);
class RealSubject implements ISubject
{
    public function request()
    {
        session_save_path(dirname(__FILE__).&#39;/sess/&#39;);
        @session_start();
        $_SESSION[&#39;bergift&#39;] = &#39;admin&#39;;
        header("Location:main.php");
    }
}
?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template