I often talk about traditional verification and thinkphp frameworks in php.

Source: Internet
Author: User

I often talk about traditional verification and thinkphp frameworks in php.

PHP (hyper-text pre-processor) can be used to build small websites. When users need to register and log on, they need to match the background database to register and log on. There are many steps in the traditional method, you need to connect to the database and then insert it with SQL statements.

<? Phpheader ("Content-type: text/html; charset = UTF-8"); $ conn = mysqli_connect ("localhost", "root", ""); if (! $ Conn) {echo "<script> alert ('Connection failed! '); History. go (-1); </script> ";} mysqli_select_db ($ conn," liuyanban "); mysqli_query ($ conn, 'set NAMES utf8 '); $ password = $ _ POST ['Password']; $ username = $ _ POST ['username']; $ face = "yellow.png"; $ result = mysqli_query ($ conn, "SELECT username from user1 where username = '$ username'"); $ a = mysqli_num_rows ($ result); if ($) {echo "<script language = javascript> alert ('user name already exists! 'Your region location.href}'reg.html '</script> ";} else {$ SQL = mysqli_query ($ conn," INSERT INTO user1 (username, password, face) VALUES ('1', '2', 'yellow.png ') "); if ($ SQL) {echo" <script language = javascript> alert ('registration successful! 'Register registration.href}'login.html '</script> ";} else {echo" <script> alert ('registration failed! 'Your region location.href}'reg.html '</script> ";}}?>

The above is a native php registration instance. You need to use functions such as mysqli_select_db () and mysqli_query () to First connect to the database. At the same time, only the mysqli_query () function can be used to execute SQL statements, finally, use the if statement to determine the category and perform other restrictions. In the native php stage, it is highly practical, easy to understand, and the process is very clear. However, in a project, using such statement code is not easy to communicate with each other, and it is very complicated, therefore, we need to use the thinkphp framework to build a project so that the coders can interact with each other and facilitate later code modification and function addition. Here we will not go into details about the framework. Therefore, in the thinkphp framework, controller (C) and model (M) are used in the mvc mode for Automatic Form Verification:

Use Form static verification in the Controller:

Public function doreg () {$ data = D ('user'); $ d = array (); $ d ['username'] = $ _ POST ['username']; $ d ['Password'] = $ _ POST ['Password']; $ d ['time'] = date ("Y-m-d H: I: s ", time (); $ d ['qq'] =_ _ POST ['qq']; $ d ['class'] =$ _ POST ['class']; $ mess = $ data-> create (); if (! $ Mess) {// Automatic Form Verification $ this-> error ($ data-> getError (), 'member/member', 3 );} else {$ data-> add (); echo "<script language = javascript> alert ('registration successful! 'Your website location.href}'member.html '</script> ";}}

The template lists the fields to be verified:

<? Php namespace Home \ Model; use Think \ Model; class UserModel extends Model {protected $ tableName = 'user '; protected $ _ validate = array (// perform static verification // array (verify field 1, verification rule, error prompt, [verification condition, additional rule, verification time]), array ('username', 'require ', 'username is required! '), Array ('username', '', 'account name already exists! ', 0, 'unique', 1), array ('repassword', 'Password',' two different passwords! ', 0, 'Confirm'), array ('qq', 'require', 'qq is required! '), Array ('qq', '', 'account name already exists! ', 0, 'unique', 1), array ('class', 'require', 'class required! '), Array ('J _ verify', 'require ', 'verification code is required! '),) ;}?>

Here we use registration as an example. logon is similar. If a verification error occurs, use $ this-> error ($ data-> getError (), 'member/member', 3 ); it is convenient to use static form verification.

The traditional verification and thinkphp framework (which must be read) in php are all the content shared by the editor. I hope you can give us a reference and support for the help house.

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.