PHP and MySQL design mode: proxy mode

Source: Internet
Author: User
Tags md5 php and mysql

  First, the database connection general class

Important Interfaces:

The interface is used to store MySQL connection data. This data can be used by classes that implement this interface.

Through the interface can isolate a simple and necessary part of the program, any program can implement this interface.

Interfaces are defined by interface and implemented through implements.

<? PHP // file name iconnectinfo.php Interface iconnectinfo{    const Host     = "localhost";     Const UserName = "root";     Const Password = "";     Const DBName   = "Bergift";          Public function doconnect ();}? >

Generic MySQL connection classes and static variables:

Interface implementations connect access values through domain resolution operators. Using private static variables to receive, you can use the speed advantage of static variable processing, but also to ensure encapsulation.

Avoid using global variables as much as possible, and global variables will break encapsulation. Static variables help reduce the instantiation of a class.

<?PHPinclude_once(' iconnectinfo.php ');classUniversalconnectImplementsiconnectinfo{Private Static $Server= Iconnectinfo::Host; Private Static $CurrentDB= Iconnectinfo::DBName; Private Static $User= Iconnectinfo::UserName; Private Static $Password= Iconnectinfo::Password; Private Static $HookUp;  Public functionDoconnect () { self::$HookUp=Mysqli_connect(Self::$Server, Self::$User, Self::$Password, Self::$CurrentDB); if(Self::$HookUp){                    }ElseIf(Mysqli_connect_error(Self::$HookUp)){            Echo"Fail:".Mysqli_connect_error; }        returnSelf::$HookUp; }}?>

A simple connection operation through a class and interface can significantly reduce development time, make it easy to modify, and store all information in constants.

  Proxy mode:

The protection agent will not send the request to a real topic until the request has been validated. The real topic is the target of the request, such as accessing the database information.

Personal understanding: The user sends the request, after the proxy module receives the request, goes to the authentication module, returns the true value correctly, the Proxy module again directs the target which the true request.

Through the general class to complete the database connection, database selection.

Common classes include: interfaces that contain database information, classes that implement database connections.

Protection agents include: interfaces, proxy modules that implement validation, and client object classes that are implemented.

Process: Login page-->client.php (implemented client object class)-->proxy.php (proxy module)--Use the generic class to connect to the database for judgment, and correctly return the proxy truth--- The agent directs the page to realproject.php for processing.

The role of the agent is to ensure that users with permissions can access the site.

Proxy participants in the proxy model can be seen as a place where they can do some really high-security operations before accessing real-world topics.

The proxy module is a simple password check that can call a high-security module to handle sensitive information.

  Interface file

<? PHP // file name isubject.php Interface isubject{    function  request ();}? >

proxy class

<?PHP//file name proxy.phpinclude_once(' isubject.php ');include_once(' realsubject.php ');include_once(' universalconnect.php ');classProxyImplementsisubject{Private $TableMaster; Private $HookUp; Private $LoginSuccess; Private $RealSubject;  Public functionLogin$UserNow,$PassNow)    {        $UserName=$UserNow; $PassWord=MD5($PassNow); $this->loginsuccess =false; $this->tablemaster = "Badmin"; $this->hookup = Universalconnect::Doconnect (); $sql= "Select password from$this->tablemaster WHERE username = '$UserName‘"; if($result=$this->hookup->query ($sql))        {            $row=$result-Fetch_array (MYSQLI_ASSOC); if($row[' Password ']==$PassWord)            {                $this->loginsuccess =true; }            $result-Close (); }        ElseIf(($result=$this->hookup->query ($sql))===false)        {            Echo"Failed".$this->hookup->error; Exit(); }        $this->hookup->Close (); if($this-loginsuccess) {            $this-request (); }        Else        {            Header("Location:index.php"); }    }     Public functionRegister$UserNow,$PassNow)    {        $UserName=$UserNow; $PassWord=MD5($PassNow); $this->loginsuccess =false; $this->tablemaster = "Badmin"; $this->hookup = Universalconnect::Doconnect (); $sql= "INSERT into$this->tablemaster VALUES ('$UserName‘,‘$PassWord‘)"; if($result=$this->hookup->query ($sql))        {            $this->loginsuccess =true; }        ElseIf(($result=$this->hookup->query ($sql))===false)        {            Echo"Failed".$this->hookup->error; Exit(); //header ("Location:index.php");        }        $this->hookup->Close (); if($this-loginsuccess) {            Echo"<script>alert (' success! '); </script> "; }        Else        {            Header("Location:index.php"); }    }     Public functionrequest () {$this->realsubject =NewRealsubject (); $this->realsubject->request (); }}?>

The client class that receives the information sent by the clients, sending the message to the proxy class for validation.

<?PHP//file name client.phpinclude_once(' proxy.php ');classclient{Private $Proxy; Private $UserName; Private $PassWord;  Public function__construct () {$this->tablemaster = ' badmin '; $this->hookup = Universalconnect::Doconnect (); $this->username =$this->hookup->real_escape_string (Trim($_post[' username '])); $this->password =$this->hookup->real_escape_string (Trim($_post[' Password '])); if($_get[' Do ']=== ' register ')        {            $this->getregis ($this->proxy=NewProxy ()); }ElseIf($_get[' Do ']=== ' login ')        {            $this->getiface ($this->proxy=NewProxy ()); }    }    Private functionGetiface (Isubject$proxy)    {        $proxy->login ($this->username,$this-PassWord); }    Private functionGetregis (Isubject$proxy)    {        $proxy->register ($this->username,$this-PassWord); }}$Worker=NewClient ();?>

  Real topic, can only be entered by the proxy class.

<?PHP//file name realsubject.phpinclude_once(' isubject.php ');classRealsubjectImplementsisubject{ Public functionrequest () {Session_save_path(dirname(__file__).‘ /sess/'); @Session_Start(); $_session[' bergift '] = ' admin '; Header("Location:main.php"); }}?>

PHP and MySQL design mode: proxy mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.