The function code of login, register and retrieve password under thinkphp framework

Source: Internet
Author: User
This article mainly introduces the thinkphp framework to achieve login, registration, retrieve password function of the relevant information, interested in small partners can refer to

In this paper, we have shared the implementation method of using AJAX form to submit login, registration, and find password in thinkphp framework, as well as the background audit for registered users.

User table field ID, num, password, name, email, addtime, status

The specific code is as follows

<?phpnamespace home\controller;use Think\controller;class Logincontroller extends Controller {//Process login public functio    n signin () {if (is_get) {$this->display ();        if (is_post) {/* Call Login Interface Login */$User = M (' User ');       The I method receives the value of the page pass $num = I (' num ');      $password = I (' password ');      Find the value of num equals $num in the User table $datanum = $User->where (array (' num ' = $num))->find (); Determine the value of $datanum if ($datanum) {if (MD5 ($password) = = = $datanum [' password ']) {if ($datanum [' status ']          = = 0) {$this->error (' User is in an unaudited state, please contact the Administrator ');          }elseif ($datanum [' status '] = = 2) {$this->error (' User is disabled, please contact Administrator ');             }else{$this->autologin ($datanum);//Call private method to log on automatically.            $uid = $datanum [' id ']; if ($_session[' User_auth ' [' uid '] && $_session[' user_auth ' [' role '] = = ' user ') {$this->success (' Login as Work!            ', U (' Index/index ')); }else{$this->error(' storage error. ');           }}}else{$this->error (' password is not filled in correctly, please re-fill ');        Exit ();      }}else{$this->error (' User not present, please register ', U (' Signup '));       }}} Public function Autologin ($user) {/* Record login session */$auth = array (' uid ' = = $user [' id '],    ' num ' = $user [' num '], ' role ' and ' user ',//record user type);    Session (' User_auth ', $auth);   Session (' User_auth_sign ', Data_auth_sign ($auth));    }/* User registration */Public Function signup () {if (Is_user_login ()) {$this->redirect (' Index/index ');    } if (Is_get) {//Register page $this->display ();       } if (Is_post) {//Judgment user $data [' num '] = I (' num ');      $User = M (' User ');      $datanum = $User->where ($data)->find ();      if ($datanum) {$this->success (' You have already registered, please log in directly ', U (' signin '));         }else{$data [' password '] = MD5 (I (' password '));        $data [' name '] = I (' name ');      $data [' email '] = I (' email ');  $data [' addtime '] = time ();        $uid = $User->add ($data);         if ($uid) $this->success (' registered success ', U (' signin '));       else $this->error (' registration failed ');      }}} Public function logout () {if (Is_user_login ()) {$User = M (' user ');      Session (' User_auth ', null);      Session (' User_auth_sign ', null);      Session (' [Destroy] '); $this->success (' Log out success!    ', U (' signin '));    } else {$this->redirect (' signin ');    }}//Forgot password public function Wjpas () {if (is_get) {$this->display ();      } if (is_post) {$User = M (' User ');      $num = I (' num ');       $data [' password '] = MD5 (I (' password '));      $email = I (' email ');      $datanum = $User->where (array (' num ' = $num))->find ();  if ($datanum) {if ($email = = = $datanum [' email ']) {$User->where (' num ' = $num) '->save ($data);         Update records according to condition $this->success (' Password modified successfully ', U (' signin ')); }else{$this->error (' email is not filled inPlease re-fill ');        Exit ();      }}else{$this->error (' User not present, please register ', U (' Signup ')); }}}}?>

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.