ThinkPHP implements login and thinkphp implements Login

Source: Internet
Author: User

ThinkPHP implements login and thinkphp implements Login

Idea: Enter the account and password at the front end, and a custom function checkNamePwd () in the background is used to verify whether the account and password are correct and called in the Controller. Among them, checkNamePwd () methods to verify that the account password is correct, first use the account to find the password, and then compare the password found with the password entered by the user. If the password is the same, the login is successful; otherwise, the login fails!

First, define a function checkNamePwd () in the model class ()

Public function checkNamePwd ($ name, $ pwd) {// ① first query whether a record with the specified name exists based on $ name // query the entire record through $ name $ res = $ this-> where ("mg_name = '$ name '") -> find (); if ($ res) {// ② compare the password of the query record with the password entered by the user if ($ res ['mg _ pwd'] ===$ pwd) {return $ res ;}} else {return null ;}}

The Controller receives user input information and calls the checkNamePwd () method.

$ Manager = new \ Model \ ManagerModel (); $ name = $ _ POST ['admin _ user']; $ pwd = $ _ POST ['admin _ psd ']; // if the verification succeeds, the entire record is returned. Otherwise, null $ info = $ manager-> checkNamePwd ($ name, $ pwd) is returned. if ($ info) {// The verification succeeds, session persistence operation (name, id) session ('admin _ id', $ info ['mg _ id']); session ('admin _ name ', $ info ['mg _ name']); // jump to the background homepage $ this-> redirect ('index/Index ');} else {echo "incorrect user name or password ";}

 

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.