PHP實現登陸代理

來源:互聯網
上載者:User
####代理模式* 組成* 抽象角色:通過介面或抽象類別生命真實角色實現的業務方法* 代理角色:實現抽象角色,是真是角色的代理* 真實角色:實現抽象角色,定義真實角色所要實現的商務邏輯#####實現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"."
"; }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"."
"; //echo $drop; if($this->hookup->query($drop) == true){ printf("Old table %s has been dropped"."
",$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(); } }}```#####代理模式實現使用者登陸```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 = << data
  1. fuck u silly b
REQUEST;echo $practice; }}$data = new Client();```
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.