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