Tp login registration, tp login Registration

Source: Internet
Author: User

Tp login registration (transfer), tp login Registration

When you log on, you can update user data, logon ip address, Logon Time, and number of logins plus 1. This makes it easy to know if it is appropriate for verification.
Source Code address: https://github.com/grh0812/thinkphp-login-register

Create a database:

/* Navicat MySQL Data Transfer Source Server: Local Connection Source Server Version: 50617 Source Host: localhost: 3306 Source Database: crm Target Server Type: MYSQLTarget Server Version: 50617 File Encoding: 65001 Date: 23:55:28 */SET FOREIGN_KEY_CHECKS = 0; -- ------------------------------ Table structure for think_users -- ---------------------------- drop table if exists 'think _ users '; create table 'think _ users' ('userid' mediumint (8) unsigned not null AUTO_INCREMENT COMMENT 'user id', 'companyid' mediumint (8) unsigned not null comment 'Company id', 'pid 'mediumint (8) not null comment 'parent id', 'username' char (20) not null default ''comment' Username ', 'Password' char (32) not null default ''comment' password', 'nickname' char (20) not null default ''comment' nickname ', 'regdate' int (10) unsigned not null comment' registration time', 'lastdate' int (10) unsigned not null comment 'last logon time', 'regip' char (15) not null default ''comment' register ip', 'lastip' char (15) not null default ''comment' last logon ip address ', 'loginnum' smallint (5) unsigned not null default '0' comment' logon Times', 'email 'char (32) not null default ''comment' mailbox, 'mobile' char (11) not null default ''comment 'mobile phone number', 'islock' tinyint (1) unsigned not null default '0' COMMENT 'locked ', 'vip 'tinyint (1) unsigned not null default '0' COMMENT 'Membership', 'overduedate' int (10) unsigned not null comment 'account expiration time', 'status' tinyint (1) unsigned not null default '0' comment' status-for soft delete ', primary key ('userid'), unique key 'username' ('username') using btree, KEY 'email '('email') using btree) ENGINE = MyISAM AUTO_INCREMENT = 5 default charset = utf8;

Create a model (for automatic verification and Automatic completion ):

Namespace Home \ Model; use Think \ Model; class UsersModel extends Model {/*** Automatic Verification ** self: EXISTS_VALIDATE or 0 fields are verified (default) * self :: MUST_VALIDATE or 1 must be verified * self: VALUE_VALIDATE or when the value of 2 is not null */protected $ _ validate = array ('nickname', 'require ', 'nickname cannot be blank! '), // Verify array ('username', 'require', 'username cannot be blank by default! '), // Verify array ('username', '',') with regular expressions by default. 'This user name has been registered! ', 0, 'unique', 1), // verify that the name field is unique when it is added ('email', '', 'this mailbox is occupied ', 0, 'unique', 1), // indicates whether the email field is unique array ('mobile', '', 'occupied by this phone number ', 0, 'unique', 1), // indicates whether the mobile field is unique when it is added. // The regular expression verification password [must contain letters and numbers and one, 6-22 characters long] array ('Password', '/^ ([a-zA-Z0-9 @ * #] {6, 22}) $/', 'incorrect password format, please enter it again! ', 0), array ('repassword', 'Password', 'Confirm the password is incorrect', 0, 'Confirm '), // verify that the password is consistent with the password. array ('email ', 'email', and 'incorrect email format '), // built-in Regular Expression verification email format array ('mobile ', '/^ 1 [34578] \ d {9} $/', 'incorrect Mobile Phone Number Format ', 0), // regular expression to verify the mobile phone number array ('verify ', 'verify _ check', 'verification code error', 0, 'function'), // you can check whether the verification code is correct. // array ('agree ', 'is _ agree ', 'Please first agree to the website security agreement! ', 1, 'callback'), // determine whether to check the website security protocol array ('agree', 'require ', 'agree to the website security protocol first! ', 1), // determine whether to check the website Security Protocol);/*** automatically completed */protected $ _ auto = array ('Password', 'md5 ', 3, 'function'), // when adding and editing the password field, make the md5 function process array ('regdate', 'time', 1, 'function '), // write the current timestamp array ('regip', 'Get _ client_ip ', 1, 'function') to the newly added regdate field '), // write the current registered IP address to the regip field when it is added);/*** determine whether to agree to the Website Security Management Protocol * @ return bool */protected function is_agree () {// get POST data $ agree = I ('Post. agree ', 0, 'intval'); // verify if ($ agree) {return true;} else {return false ;}}

Login registration:

Namespace Home \ Controller; use Think \ Controller; /*** Class LoginController * @ package Home \ Controller */class LoginController extends Controller {/*** User Logon */public function login () {// determine the submission method if (IS_POST) {// instantiate the Login object $ login = D ('login'); // automatically verify if (! $ Data = $ login-> create () {// prevents output of Chinese garbled headers ("Content-type: text/html; charset = UTF-8 "); exit ($ login-> getError ();} // Combined Query condition $ where = array (); $ where ['username'] = $ data ['username']; $ result = $ login-> where ($ where)-> field ('userid, username, nickname, password, lastdate, lastip')-> find (); // verify the username and password. if ($ result & $ result ['Password'] = $ result ['Password']) {// store session ('uid ', $ result ['U Serid']); // session of the current user ID ('nickname', $ result ['nickname']); // session of the current user nickname ('username ', $ result ['username']); // session of the current user name ('lastdate', $ result ['lastdate']); // session of the Last Logon Time ('lastip ', $ result ['lastip']); // The Last Logon ip address // updates User Logon Information $ where ['userid'] = session ('uid '); M ('users')-> where ($ where)-> setInc ('loginnum'); // The number of Logon times plus 1 M ('users ') -> where ($ where)-> save ($ data); // update the logon time and logon ip address $ this-> success ('Logon successful. The system homepage is displayed... ', U ('index/Index');} else {$ this-> error ('logon failed, incorrect user name or password! ') ;}} Else {$ this-> display () ;}/ *** user registration */public function register () {// determine the submission method for different processing if (IS_POST) {// instantiate the User object $ user = D ('users'); // automatically verifies the creation of the dataset if (! $ Data = $ user-> create () {// prevents output of Chinese garbled headers ("Content-type: text/html; charset = UTF-8 "); exit ($ user-> getError ();} // Insert the database if ($ id = $ user-> add ($ data )) {/* directly register the user as a super administrator. subusers use the invitation registration mode. Then, set the company id to the registered user id, easy to manage company users */$ user-> where ("userid = $ id")-> setField ('companyid', $ id ); $ this-> success ('registration successfully', U ('index/Index'), 2);} else {$ this-> error ('registration failed ');}} else {$ this-> display () ;}/ *** user logout */public function logout () {// clear all session sessions (null ); redirect (U ('login/login'), 2, 'logging out... ');}/*** Verification Code */public function verify () {// instantiate the Verify object $ verify = new \ Think \ Verify (); // configure the verification code parameter $ verify-> fontSize = 14; // The Verification Code font size $ verify-> length = 4; // The number of digits of the Verification Code $ verify-> imageH = 34; // Verification Code height $ verify-> useImgBg = true; // enable the verification code background $ verify-> useNoise = false; // disable the verification code interference miscellaneous $ verify-> entry ();}}

Logon template:

<Form action = "_ SELF _" method = "post"> <div class = "form-group has-feedback"> <input type = "text" name =" username "class =" form-control "placeholder =" username "/> <span class =" glyphicon-user form-control-feedback "> </span> </div> <div class = "form-group has-feedback"> <input type = "password" name = "password" class = "form-control" placeholder = "password"/> <span class = "glyphicon-lock form-control-fe Edback "> </span> </div> <div class =" form-group has-feedback "> <input type =" text "name =" verify "class =" form -control "placeholder =" Verification Code "style =" width: 200px; "/> <span class =" glyphicon-qrcode form-control-feedback "style =" right: 120px; "> </span>  </div> <div class =" row "> <div class =" col-xs-8 "> <div class =" checkbox icheck "> <label> <input type =" checkbox "name =" remember "value =" 1 "> remember me </label> </div> <! --/. Col --> <div class = "col-xs-4"> <button type = "submit" class = "btn-primary btn-block btn-flat"> login </button> </ div> <! --/. Col --> </div> </form>

Registration template:

<Div class = "register-box-body"> <p class = "login-box-msg"> register a new user </p> <form action = "_ SELF _ "method =" post "> <div class =" form-group has-feedback "> <input type =" text "name =" nickname "class =" form-control "placeholder =" nickname "/> <span class =" glyphicon-leaf form-control-feedback "> </span> </div> <div class =" form-group has-feedback "> <input type =" text "name =" username "class =" form-control "placehold Er = "username"/> <span class = "glyphicon-user form-control-feedback"> </span> </div> <div class = "form-group has -feedback "> <input type =" password "name =" password "class =" form-control "placeholder =" password "/> <span class =" glyphicon-credit- card form-control-feedback "> </span> </div> <div class =" form-group has-feedback "> <input type =" password "name =" repassword "class =" form-control "placeholder = "Confirm Password"/> <span class = "glyphicon-check form-control-feedback"> </span> </div> <div class = "form-group has -feedback "> <input type =" email "name =" email "class =" form-control "placeholder =" email "/> <span class =" glyphicon-envelope form -control-feedback "> </span> </div> <div class =" form-group has-feedback "> <input type =" text "name =" mobile "class = "form-control" placeholder = "mobile phone number"/> <span Class = "glyphicon-phone form-control-feedback"> </span> </div> <div class = "form-group has-feedback"> <input type =" text "name =" verify "class =" form-control "placeholder =" Verification Code "style =" width: 200px; "/> <span class =" glyphicon-qrcode form-control-feedback "style =" right: 120px; "> </span>  </div> <div class =" row "> <div class =" col-xs-8 "> <div class =" checkbox icheck "> <label> <input type =" checkbox "name =" agree "value =" 1 "> I agree to the <a href =" # "> Website Security Agreement </a> </label> </div> </ div> <! --/. Col --> <div class = "col-xs-4"> <button type = "submit" class = "btn-primary btn-block btn-flat"> click to register </button> </div> <! --/. Col --> </div> </form> <a href = "login.html" class = "text-center"> I have registered an account. </a> </div>

 

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.