标签:
代理模式是一种结构型设计模式, 为其他对象提供一种代理,并以控制对这个对象的访问。而对一个对象进行访问控制的一个原因是为了只有在我们确实需要这个对象时才对它进行创建和初始化。
<?php
interface IConnect
{
const HOST = ‘localhost‘;
const USER_NAME = "root";
const PASSWORD = ‘111111‘;
const DB_NAME = ‘test‘;
public static function doConnect();
}
<?php
include_once(‘IConnect.php‘);
class UniversalConnect implements IConnect
{
private static $server = IConnect::HOST;
private static $currentDB = IConnect::DB_NAME;
private static $user = IConnect::USER_NAME;
private static $pass = IConnect::PASSWORD;
private static $hookup;
public static function doConnect()
{
self::$hookup = mysqli_connect(self::$server, self::$user, self::$pass, self::$currentDB);
if(self::$hookup)
{
//echo "成功连接到MySQL";
}
else if(mysqli_connect_error(self::$hookup))
{
echo("这里是为什么连接错误:" . mysqli_connect_error());
}
return self::$hookup;
}
}
<?php
include_once(‘UniversalConnect.php‘);
class ConnectClient
{
private $hookup;
public function __construct()
{
$this->hookup = UniversalConnect::doConnect();
}
}
$worker = new ConnectClient();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1></h1>
<section>
<article>
<form action="Register.php" method="post">
用户名:<input type="text" name="username"><br>
密码: <input type="text" name="password"><br>
<input type="submit" value="注册">
</form>
</article>
</section>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1></h1>
<section>
<article>
<form action="Client.php" method="post">
用户名:<input type="text" name="username"><br>
密码: <input type="text" name="password"><br>
<input type="submit" value="登录">
</form>
</article>
</section>
</body>
</html>
<?php
include_once(‘UniversalConnect.php‘);
class CreateTable
{
private $tableName;
private $hookup;
public function __construct()
{
$this->tableName = ‘proxyLog‘;
$this->hookup = UniversalConnect::doConnect();
$dropSql = "DROP TABLE IF EXISTS $this->tableName";
if($this->hookup->query($dropSql) === true)
{
printf("旧表%s已经被删除<br />", $this->tableName);
}
$sql = "CREATE TABLE $this->tableName (username VARCHAR(15),password VARCHAR(120))";
if($this->hookup->query($sql) === true)
{
printf("表%s已经被建立<br />", $this->tableName);
}
$this->hookup->close();
}
}
$worker = new CreateTable();
<?php
include_once(‘UniversalConnect.php‘);
class Register
{
private $tableName;
private $hookup;
public function __construct()
{
$this->tableName = ‘proxyLog‘;
$this->hookup = UniversalConnect::doConnect();
$username = $this->hookup->real_escape_string(trim($_POST[‘username‘]));
$password = $this->hookup->real_escape_string(trim($_POST[‘password‘]));
$sql = "INSERT INTO $this->tableName (username, password) VALUES (‘$username‘, md5(‘$password‘))";
if($this->hookup->query($sql) === true)
{
echo ‘注册成功‘;
}
else
{
printf("无效的请求: %s<br /> SQL: %s", $this->hookup->error, $sql);
$this->hookup->close();
}
}
}
$worker = new Register();
<?php
include_once(‘Proxy.php‘);
class Client
{
private $tableName;
private $hookup;
private $proxy;
private $username;
private $password;
public function __construct()
{
$this->tableName = ‘proxyLog‘;
$this->hookup = UniversalConnect::doConnect();
$this->username = $this->hookup->real_escape_string(trim($_POST[‘username‘]));
$this->password = $this->hookup->real_escape_string(trim($_POST[‘password‘]));
$this->getIface($this->proxy = new Proxy());
}
private function getIface(ISubject $proxy)
{
$proxy->login($this->username, $this->password);
}
}
$worker = new Client();
<?php
interface ISubject
{
function request();
}
<?php
include_once(‘ISubject.php‘);
include_once(‘RealSubject.php‘);
include_once(‘UniversalConnect.php‘);
class Proxy implements ISubject
{
private $tableName;
private $hookeup;
private $logGood;
private $realSubject;
public function login($username, $passwrd)
{
$passwrd = md5($passwrd);
$this->logGood = false;
//选择表和连接
$this->tableName = ‘proxyLog‘;
$this->hookeup = UniversalConnect::doConnect();
//创建SQL语句
$sql = "SELECT password FROM $this->tableName WHERE username=‘$username‘";
if($result = $this->hookeup->query($sql))
{
$row = $result->fetch_array(MYSQL_ASSOC);
if($row[‘password‘] == $passwrd)
{
$this->logGood = true;
}
$result->close();
}
else
{
printf("失败: %s<br />", $this->hookeup->error);
}
$this->hookeup->close();
if($this->logGood)
{
$this->request();
}
else
{
echo "用户名或密码错误<br >";
}
}
public function request()
{
$this->realSubject = new RealSubject();
$this->realSubject->request();
}
}
<?php
include_once(‘ISubject.php‘);
class RealSubject
{
public function request()
{
echo ‘这里是真实的内容‘;
}
}
标签:
原文地址:http://www.cnblogs.com/chenqionghe/p/4816579.html