Home > Backend Development > PHP Tutorial > PHP实现登陆代理

PHP实现登陆代理

WBOY
Release: 2016-06-20 12:34:52
Original
1096 people have browsed it

####代理模式* 组成* 抽象角色:通过接口或抽象类生命真实角色实现的业务方法* 代理角色:实现抽象角色,是真是角色的代理* 真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑#####实现Mysql连接类```/** * Created by PhpStorm. * User: gewenrui * Date: 16/3/13 * Time: 上午8:16 */namespace db;//php允许借口存储常量interface IConnectInfo{    //服务器名称    const HOST     = "localhost";    //用户名    const USERNAME = "root";    //密码    const PASSWORD = "";    //数据库名称444444444444444444444    const DBNAME   ="proxylog";    public function doConnect();}class UniversalConnect implements IConnectInfo{    //实现了作用域解析操作符来访问连接值    //静态变量处理速度优势,提供私有可见性提供的封装性    private static $server    = IConnectInfo::HOST;    private static $currentDB = IConnectInfo::DBNAME;    private static $username  = IConnectInfo::USERNAME;    private static $password  = IConnectInfo::PASSWORD;    private static $hookup;    public  function doConnect()    {        //连接数据库        self::$hookup = mysqli_connect(self::$server,self::$username,self::$password,self::$currentDB);        if(self::$hookup){            echo "Successfucl connection to MYSQL"."</br>";        }else if(mysqli_connect_error(self::$hookup)){            echo "".mysqli_connect_error();        }        return self::$hookup;    }}```######进行新建表操作```class CreateTable{    //设置表名    private  $tableMaster = "proxyLog";    private  $hookup;    public function __construct()    {        $this->tableMaster = "proxyLog";        $this->hookup = UniversalConnect::doConnect();        $drop = "DROP TABLE IF EXITS $this->tableMaster"."</br>";        //echo $drop;        if($this->hookup->query($drop) == true){            printf("Old table %s has been dropped"."</br>",$this->tableMaster);        }        $sql = "CREATE TABLE $this->tableMaster (uname VARCHAR(15),pw VARCHAR(120))";        //echo $sql;        if($this->hookup->query($sql) == true){            echo "Table{$this->tableMaster} has been created successful";        }        $this->hookup->close();    }}```######向表中添加数据```class HashRegister{    public function __construct()    {        $this->tableMaster = "proxyLog";        $this->hookup = UniversalConnect::doConnect();        $username = $this->hookup->real_escape_string(trim($_POST['uname']));        $password = $this->hookup->real_escape_string(trim($_POST['pw']));        $sql = "INSERT INTO $this->tableMaster(uname,pw) VALUES('$username',md5('$password'))";        echo $sql;        if($this->hookup->query($sql)){            echo "registeration conpleted";        }else if($result = $this->hookup->query($sql) == false){            printf("Invalid query : %s while query %s");            exit();            $this->hookup->close();        }    }}```#####代理模式实现用户登陆```<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/3/13 * Time: 上午10:35 */include "demo26.php";interface ISubject{    function request();}//生成一个客户端接受用户post的数据//实现登陆代理class Client{    private $proxy;    private $un;    private $pw;    public function __construct()    {        $this->tableMaster = "proxyLog";        $this->hookup = \db\UniversalConnect::doConnect();        $this->un = $this->hookup->real_escape_string(trim($_POST['uname']));        $this->pw = $this->hookup->real_escape_string(trim($_POST['pw']));        //代理模式        $this->getIface($this->proxy = new Proxy());    }    public function getIface(ISubject $proxy){        $proxy->login($this->un,$this->pw);    }}//代理类class Proxy implements ISubject{    private $tableMaster;    private $hookup;    private $logGood;    private $realSubject;    public  function login($uNow,$pwNow){        $uname = $uNow;        $pw    = md5($pwNow);        $this->logGood = false;        $this->tableMaster = "proxyLog";        $this->hookup = \db\UniversalConnect::doConnect();        $sql = "SELECT pw from $this->tableMaster WHERE uname = '$uname'";        if($result = $this->hookup->query($sql)){            $row = $result->fetch_array(MYSQLI_ASSOC);            echo $pw;            if($row['pw'] == $pw){                $this->logGood = true;            }            $result->close();        }else if(($result = $this->hookup->query($sql)===false)){            printf("failed %s",$this->hookup->error);            exit();        }        $this->hookup->close();        if($this->logGood){            //执行request方法            $this->request();        }else{            echo "username and/or password not on record";        }    }    public function request()    {        $this->realSubject = new RealSubject();        $this->realSubject ->request();    }}class RealSubject implements ISubject{    public function request()    {      $practice = <<<REQUEST        <html>        <head><body><head> data </head>        <body>        <ol>        <li>fuck u silly b</li>        </ol>        </body>        </html>REQUEST;echo $practice;    }}$data = new Client();```
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template