ThinkPHP hand-held quick website splicing on the fifth day (5)

Source: Internet
Author: User
: This article mainly introduces ThinkPHP hand-held quick link website (5) on the fifth day. if you are interested in PHP tutorials, refer to it. June 11, Sunny. "Plum leaves sour soft teeth, and banana leaves Green and window screens. ."

10. background writing-continued-2

1. rewrite the Login method of the login module. change M to D.

 Assign ('title', 'background management system'); $ this-> display () ;}// function login () {header ("Content-Type: text/html; charset = utf-8 "); $ username = $ _ POST ['username']; $ password = md5 ($ _ POST ['password']); $ User = D ("User"); // The User of the parameter must be capitalized; otherwise, the automatic verification function is invalid! If (! $ User-> create () {$ this-> error ($ User-> getError ());} else {// check whether the input username exists if ($ User-> where ("username = '$ username' AND password =' $ password'")-> find ()) {session (username, $ username); $ url = U ('/Index/index/username /'. $ username); redirect ($ url, 5, 'Jump... ') ;}else {$ this-> error ('user name or password error ');}}}
Where, $User = D('User');Indicates Instantiating the User Model will import the Lib/Model/UserModel. class. php file under the current project, and then instantiate the UserModel class. Therefore, the actual code may be equivalent to the following:
                               import('@.Model.UserModel');                               $User = new UserModel();
After you create a data object, create () automatically collects the submitted form data. Before writing a form into a data table, there are often some checks on the data (whether the submitted user name meets the requirements) and processing (such as the verification code in the example ). The create () method supports automatic data verification and completion.

Generally, when a model is instantiated using the M method, the user-defined model class cannot be instantiated, and automatic verification and completion cannot be used. Therefore, we recommend that you use the D method to instantiate the model class.

2. Compile the custom User Model class admin/Lib/Model/UserModel. class. php

 15 | strlen ($ data) <5) {return false;} return true;} // the verification code callback function (ThinkPHP will automatically pass parameters for us) function callback_checkCode ($ data) {if (md5 ($ data )! = $ _ SESSION ['verify ']) {return false;} return true;} // automatic completion, automatically executed at create // array ('fill field ', 'fill content', 'fill condition', 'Add rule'); // fill field protected $ _ auto = array ('password', 'md5', 3, 'function'), array ('IP', 'callback _ returnip', 1, 'callback'), array ('createtime', 'time', 1, 'function'),); function callback_returnip () {return $ _ SERVER ['remote _ ADDR '] ;}}?>
The verification rules are defined as uniform rules in the following format:
Array (verification field 1, verification rule, error prompt, [verification condition, additional rule, verification time]), array (verification field 2, verification rule, error prompt, [verification conditions, adding rules, verification time]),...);
Description
Verification field: (required) name of the form field to be verified. This field is not necessarily a database field, it can also be some auxiliary fields of the form, such as confirming the password and verification code. If some verification rules have nothing to do with fields, the verification fields can be set at will. for example, the expire validity period rules are irrelevant to form fields. If field ING is defined, the verification field name here should be the actual data table field rather than the form field.
Verification rules: (required) rules to be verified must be combined with additional rules. if you use additional rules for regular expression verification, the system also has built-in rules for common regular expression verification, it can be used directly as a verification rule, including: The require field is required, the email address, the url address, the currency of currency, and the number.
Prompt information: (required) defines the prompt information after verification fails.
Verification conditions: (optional) the conditions are as follows:
Model: EXISTS_VALIDATE or 0 (default)
Model: MUST_VALIDATE or 1 must be verified
Verification when Model: VALUE_VALIDATE or 2 is not null

3. Compile the database table think_news

CREATE TABLE `think_news` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `author` int(11) NOT NULL, `subject` varbinary(256) NOT NULL, `createtime` int(11) NOT NULL, `lastmodifytime` int(11) NOT NULL,  `message` mediumtext NOT NULL,   PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

5. modify admin \ Tpl \ Index \ index.html

The edit button is as follows:

 

  • Edit news
  • '/Public' indicates Public Site Directory,'/index.php/Article'The URL of the current module.

    6. add the function news in admin \ Lib \ Action \ IndexAction. class. php.

    Function news () {// jump to the index method redirect (U ('/News/index') of the News controller, 0, 'compile News ');}

    The preceding section describes ThinkPHP's hand-held quick link to the website (5) on the fifth day, including some content. I hope my friends who are interested in PHP tutorials will be helpful.

    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.