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

PHP实现登陆代理

Jun 20, 2016 pm 12:34 PM

1

####代理模式* 组成* 抽象角色:通过接口或抽象类生命真实角色实现的业务方法* 代理角色:实现抽象角色,是真是角色的代理* 真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑#####实现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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles