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 ";}