thinkphp Framework Forms Automatic Authentication login Registration AJAX automatic Authentication login Registration

Source: Internet
Author: User
Tags php class

Description: There is no practice of static automatic verification: If you use static authentication to customize a controller first, and then create a nihaomodel.php class in the Model folder Nihao is custom, the prefix can be arbitrary, but must use the Hump method (first letter capital)

When using the D () method in a custom controller, the prefix of the Nihaomodel.php class is written Nihao because there is this string of code protected $trueTableName = ' Yonghu '; The function is to find the real table name, the system by default find Yong_hu This table, need to define this method, to find the Yonghu table

<?phpnamespace admin\model;use think\model;class Nihaomodel extends model{    //protected $tablePrefix = "";    protected $trueTableName = ' Yonghu '; The function is to find the real table name, the system by default find Yong_hu This table, need to define this method, to find Yonghu table    protected $_validate =array (//fixed mode, in the parent class has defined an        array (' UID ', ' Require ', ' user name cannot be empty! ', 0,//non-null authentication: Give Yonghu the UID column in the table, set non-empty        array (' pwd ', ' pwd1 ', ' Input password inconsistent ', 0, ' confirm '),//equivalence authentication: Confirm (Verify that the two fields in the form are the same, the validation rule defined is a field name), meaning that pwd1 must be a field        array (' email ', ' email ', ' Mailbox format incorrect ', 0 '),        Array (' name ', '/ ^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID number incorrect ', 0, ' regex '),//Regular expression,        array (' Age ', ' 18,50 ', ' ages not in range ', 0, ' between ')//range Validation:    );}

Dynamic validation: (No model models needed)

1. Build a controller to do form operation (including validation)

<?phpnamespace biaodan\controller;use think\controller;class Biaodancontroller extends Controller{public function            Test () {if (empty ($_post))//If $_post empty, displays the add page, {$this->show ();            } else//If $_post is not empty, go to verify, verify success, add database {$y = D ("Yonghu"); $arr = Array (//Create a validation rule for array (' UID ', ' require ', ' username cannot be empty! ', 0), array (' pwd ', ' pwd1 ', ' input passwords inconsistent ', 0,                ' Confirm '),//Equivalence Authentication: Confirm (Verify that the two fields in the form are the same, the validation rule defined is a field name), meaning that the pwd1 must be a field array (' email ', ' email ', ' email ' is not properly formatted ', 0), Array (' name ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]|            X) $/', ' ID number incorrect ', 0, ' regex '),//Regular expression, array (' Age ', ' 18,50 ', ' ages not in range ', 0, ' between ')//range validation);                if ($y->validate ($arr)->create ())//$y->validate ($arr), add before executing the validation rule {$y->add ();        } else {die ($y->geterror ());        }        }    }    } 

2. HTML page corresponding to the Controller method:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Ajax: (No model models needed)

1. Build a controller to do form operation (including validation)

<?phpnamespace admin\controller;use Think\controller;class Zhucecontroller extends Controller {public        function Add () {                $this->show ();            }        Public Function Addchuli () {        $y = D ("Yonghubiao");        $arr = Array (        ' uid ', ' require ', ' username cannot be empty!! '),//ps:require means the field must be, is not an empty        array (' pwd ', ' require ', ' two times password inconsistent! '),        Array (' name ', ' require ', ' did not fill in the name!! '),        array (' email ', ' email ', ' email ' format is incorrect!! '),        Array (' Age ', '/^[1-9]\d{5}[1-9]\d{3} ((0\d) | ( 1[0-2]) (([0|1|2]\d) |3[0-1]) \d{3} ([0-9]| X) $/', ' ID card not valid ', 0, ' regex ')        ;                if ($y->validate ($arr)->create ()) {            $y->add ();            $this->ajaxreturn ("Registered success", "eval");        } else{            $this->ajaxreturn ($y->geterror (), "eval");}}}    

2. HTML page corresponding to the Controller method:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

thinkphp Framework Forms Automatic Authentication login Registration AJAX automatic Authentication login Registration

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.