PHP login, registration and password modification function analysis, php password Modification

Source: Internet
Author: User

PHP login, registration and password modification function analysis, php password Modification

This example describes how to implement the login, registration, and password modification functions in PHP. We will share this with you for your reference. The details are as follows:

Here we will introduce the page layout and function implementation of registration, logon, and password change:

1. log on

2. forgot password

3. Free Registration

Page Layout:

<Div id = "views" class = "views"> <div id = "view-login" class = "page-view-login active"> <present name = "wxuser "> <div id =" wxuser "class =" form-group text-center "> <div>  </div> 

Js processing:

<Script type = "text/javascript"> var tel = ''; $ (function () {var check = {checkPwd: function (password) {if (typeof password = 'undefined' | password = '') {return false;} return true ;}, checkSmscode: function (code) {if (typeof code = 'undefined' | code = '') {return false;} return true ;}, validTel: function (value) {return/^ (\ + 86) | (86 ))? (1) \ d {10} $ /. test (''+ value) ;}// login $ (". js-login "). click (function () {var tel = $ ("# login "). find ("input [name = 'tel']"). val (); if (! Check. validTel (tel) {comment ('.js-help-info'character .html ('Enter the correct mobile phone number '); // *** the system prompts that return false is still displayed on the next page ;} var password = $ ("# login "). find ("input [name = 'Password']"). val (); if (! Check. checkPwd (password) {cipher ('.js-help-info'cipher .html ('Enter the password'); return false ;}$ ('. js-login '). attr ("disabled", "disabled"); $. ajax ({url: "{sh: U ('home/userlogin')}", type: 'post', dataType: "json", data: {tel: tel, password: password}, success: function (response) {if (response. result) {location. href = response. href;} else {setTimeout (function () {$ ('. js-login '). removeAttr ("disabled") ;}, 500); Response ('.js-help-info'response .html (response. error) ;}}, error: function () {failed ('.js-help-info'failed .html ("request failed") ;}}); // register $ (". js-register "). click (function () {var tel = $ ("# register "). find ("input [name = 'tel']"). val (); if (! Check. validTel (tel) {comment ('.js-help-info'character .html ('Enter the correct mobile phone number '); // *** the system prompts that return false is still displayed on the next page ;} var password = $ ("# register input [name = 'Password']"). val (); var smscode = $ ("# register input [name = 'smscode']"). val (); var re_password = $ ("# register input [name ='re _ password']"). val (); if (! Check. checkSmscode (smscode) {response ('.js-help-info'verification .html ('Enter the verification code '); return false;} if (! Check. checkPwd (password) {login ('.js-help-info'login .html ('enter your login password'); return false;} if (! Check. checkPwd (re_password) {response ('.js-help-info'{.html ('Enter the password to confirm '); return false;} else if (password! = Re_password) {comment ('.js-help-info'character .html ('inconsistent passwords entered twice '); return false ;}$ ('. js-login '). attr ("disabled", "disabled"); $. ajax ({url: "{sh: U ('home/userRegister ')}", type: 'post', dataType: "json", data: {tel: tel, password: password, smscode: smscode}, success: function (response) {if (response. result) {location. href = response. href;} else {setTimeout (function () {$ ('. js-login '). removeAttr ("disa Bled ") ;}, 500); response ('.js-help-info'response .html (response. error) ;}}, error: function () {response ('.js-help-info'failed .html ("request failed") ;}}); // send verification code $ ('. js-sms-Code '). click (function () {var tel = $ ('# register # tel_num '). val (); if (! Check. validTel (tel) {certificate ('.js-help-info'example .html ('Enter the correct mobile phone number '); // *** the system prompts that return false is still displayed on the next page;} // check whether the account has been registered $. ajax ({url: "{sh: U ('home/checkTel ')}", type: 'post', dataType: "json", async: false, data: {tel: tel}, success: function (json) {checkRes = json. status ;}, error: function (json) {response ('.js-help-info'failed .html ("failed to send") ;}}); if (checkRes = 1) {response ('.js-help-info'{.html ("already registered user "); return false;} if (CheckRes = 3) {response ('.js-help-info'response .html ("Incorrect request"); return false ;}$ (this ). attr ("disabled", "disabled" ).html ("<span style = 'color: #666 '> <span id = 'countdown'> 60 </span> s and try again </span> "); countdown (); $. ajax ({url: "{sh: U ('home/sendsmscode')}", type: 'post', dataType: "json", data: {tel: tel}, success: function () {}, error: function () {failed ('.js-help-info'failed .html ("failed to send") ;}}); // change the password $ ('. go-forget '). Click (function () {var tel = $ ('# login # tel_num '). val (); $ ("# login "). hide (); $ ("# register "). hide (); $ ("# changePwd "). show (); $ ("# changePwd # tel_num "). val (tel ). focus (); ('.js-help-info'{.html ('') ;}); // register $ ('. go-register '). click (function () {var tel = $ ('# login # tel_num '). val (); $ ("# login "). hide (); $ ("# changePwd "). hide (); $ ("# register "). show (); $ ("# register # tel_num "). val (tel ). focus (); $ ('. js-he Lp-info'{.html ('') ;}); // log on to $ ('# changePwd. go-login '). click (function () {var tel = $ ('# changePwd # tel_num '). val (); $ ("# register "). hide (); $ ("# changePwd "). hide (); $ ("# login "). show (); $ ("# login # tel_num "). val (tel ). focus (); values ('.js-help-info'example .html ('') ;}); // log on to $ ('# register. go-login '). click (function () {var tel = $ ('# register # tel_num '). val (); $ ("# register "). hide (); $ ("# changePwd "). hide (); $ ("# Login "). show (); $ ("# login # tel_num "). val (tel ). focus (); values ('.js-help-info'{.html ('') ;}); $ ('. js-changePwd '). click (function () {var tel = $ ("# changePwd "). find ("input [name = 'tel']"). val (); if (! Check. validTel (tel) {comment ('.js-help-info'character .html ('Enter the correct mobile phone number '); // *** the system prompts that return false is still displayed on the next page ;} var password = $ ("# changePwd input [name = 'Password']"). val (); var smscode = $ ("# changePwd input [name = 'smscode']"). val (); var re_password = $ ("# changePwd input [name ='re _ password']"). val (); if (! Check. checkSmscode (smscode) {$ ('# changePwd .js-help-info'verification .html ('Enter the verification code'); return false;} if (! Check. checkPwd (password) {$ ('# changePwd .js-help-info'0000.html ('enter a new password'); return false;} if (! Check. checkPwd (re_password) {$ ('# changePwd .js-help-info'0000.html ('Enter the password to confirm'); return false;} else if (password! = Re_password) {$ ('# changePwd .js-help-info'mongo.html ('inconsistent passwords entered twice'); return false;} $. ajax ({url: "{sh: U ('home/changePwd ')}", type: "POST", dataType: "json", data: {tel: tel, password: password, smscode: smscode}, success: function (response) {if (response. result) {location. href = response. href;} else {setTimeout (function () {$ ('. js-login '). removeAttr ("disabled"); }, 500); $ ('. js-help-info '). h Tml (response. error) ;}}, error: function () {response ('.js-help-info'failed .html ("request failed") ;}}); // send an SMS to change the password $ ('. js-sms-excode '). click (function () {var tel = $ ('# changePwd # tel_num '). val (); if (! Check. validTel (tel) {certificate ('.js-help-info'example .html ('Enter the correct mobile phone number '); // *** the system prompts that return false is still displayed on the next page;} // check whether the account has been registered $. ajax ({url: "{sh: U ('home/checkTel ')}", type: 'post', dataType: "json", async: false, data: {tel: tel}, success: function (json) {checkRes = json. status ;}, error: function (json) {response ('.js-help-info'failed .html ("failed to send") ;}}); if (checkRes = 2) {response ('.js-help-info'registr.html ("number not registered "); return false;} if (checkRes = 3) {response ('.js-help-info'{.html ("Incorrect request"); return false ;}$ (this ). attr ("disabled", "disabled" ).html ("<span style = 'color: #666 '> <span id = 'countdown'> 60 </span> s and try again </span> "); countdown (); $. ajax ({url: "{sh: U ('home/sendSmsexcode ')}", type: 'post', dataType: "json", data: {tel: tel}, success: function (data) {}, error: function () {failed ('.js-help-info'failed .html ("request failed") ;}}) ;}); function countdown () {// recursive Verification Code countdown setTimeout (function () {var time = $ ("# countdown "). text (); if (time = 1) {$ ('. js-sms-Code '). removeAttr ("disabled"); Certificate ('.js-sms-code'verification .html ("Send verification code"); $ ('. js-sms-excode '). removeAttr ("disabled"); Certificate ('.js-sms-excode'verification code .html ("Send verification code");} else {$ ("# countdown "). text (time-1); countdown () ;}, 1000) ;}</script>

Php background processing:

// Log on to the public function userLogin () {if (IS_AJAX &&! $ This-> member) {$ tel = $ this-> _ post ('tel', 'trim'); $ password = $ this-> _ post ('Password ', 'trim, md5'); $ member = M ('member')-> where (array ('tel '=> $ tel)-> find (); if ($ member & $ member ['Password'] ===$ password) {// check whether a user needs to bind if ($ member ['wxuser _ id'] = 0 & $ this-> wxuser) {M ('Member ') -> where (array ('id' => $ member ['id']) -> save (array ('wxuser _ id' => $ this-> wxuser_id);} $ href = session (LASTREQ UEST); session (MEMBER, $ member ['id']); session (LASTREQUEST, null); $ this-> ajaxReturn (array ('result' => true, 'href '=> $ href? $ Href: U ('Member/Index');} else {if (empty ($ Member )) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'the mobile phone number has not been registered. ');} else {$ this-> ajaxReturn (array ('result' => false, 'error' =>' the password is incorrect. ') ;}} else {$ this-> ajaxReturn (array ('result' => false, 'error' =>' illegal request. ');} // The user exits the public function userLogout () {session (WXUSER, null); session (MEMBER, null ); $ this-> success ('exit successful ', U ('store/Me Mber/Index');} // user registers public function userRegister () {$ tel = $ this-> _ post ('tel ', 'trim '); $ password = $ this-> _ post ('Password', 'trim, md5'); $ smscode = $ this-> _ post ('smscode', 'trim '); $ session_smscode = session ($ this-> smscode); $ user_exit = M ('member')-> where (array ('tel' => $ tel )) -> find (); if (! Preg_match ("/1 [3458] {1} \ d {9} $/", $ tel) & $ user_exit) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'invalid mobile phone number ');} $ memberModel = M ('member '); // check whether $ member = $ memberModel-> where (array ('tel '=> $ tel, 'status' => 1)-> find (); if (! Empty ($ member) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'already registered user '));} if (time ()> $ session_smscode ['time'] | $ smscode! = $ Session_smscode ['code']) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'incorrect Verification code ')); // -- debug, disable the verification function first} $ data = array ('tel' => $ tel, 'Password' => $ password, 'wxuser _ id' => intval ($ this-> wxuser_id), 'addtime' => time (); $ insert_id = $ memberModel-> add ($ data ); if ($ insert_id) {$ href = session (LASTREQUEST); session (MEMBER, $ insert_id ); // ***** is only an id value $ this-> ajaxReturn (array ('result' => tru E, 'href '=> $ href? $ Href: U ('Member/Index');} else {$ this-> ajaxReturn (array ('result' => false, 'error' => 'Operation failed', 'msg '=> M ('member')-> getError ()));}} // change the password public function changePwd () {$ tel = $ this-> _ post ('tel', 'trim '); $ password = $ this-> _ post ('Password', 'trim'); $ smscode = $ this-> _ post ('smscode', 'trim '); $ session_smscode = session ($ this-> smscode); if (time ()> $ session_smscode ['time'] | $ smscode! = $ Session_smscode ['code']) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'incorrect Verification code ')); // -- debugging successful} $ data = array ('Password' => md5 ($ password), 'addtime' => time ()); $ memberModel = M ('member'); // check whether registration is successful. $ Member = $ memberModel-> where (array ('tel' => $ tel, 'status' => 1)-> find (); if (empty ($ member) {$ this-> ajaxReturn (array ('result' => false, 'error' => 'Number not registered ');} if ($ memberModel-> where (ar Ray ('tel' => $ tel)-> save ($ data) {$ href = session (LASTREQUEST); session (MEMBER, $ member ['id']); $ this-> ajaxReturn (array ('result' => true, 'href '=> $ href? $ Href: U ('Member/Index');} else {$ this-> ajaxReturn (array ('result' => false, 'error' => 'Operation failed', 'msg '=> M ('member')-> getError ()));}} // ajax check whether the number registers for public function checkTel () {$ tel = $ this-> _ post ('tel', 'trim '); if (IS_AJAX & preg_match ("/1 [3458] {1} \ d {9} $/", $ tel) {$ memberModel = M ('member '); $ member = $ memberModel-> where (array ('tel '=> $ tel, 'status' => 1)-> find (); if (! Empty ($ member) {$ this-> ajaxReturn (array ('status' => 1, 'info' => 'registered '));} else {$ this-> ajaxReturn (array ('status' => 2, 'info' => 'unregistered '));}} else {$ this-> ajaxReturn (array ('status' => 3, 'info' => 'bad request '));}} // send the registration verification code public function sendSmscode () {session ($ this-> smstime, null); $ smstime = session ($ this-> smstime ); $ tel = $ this-> _ post ('tel', 'trim'); if (IS_AJAX &&(! $ Smstime | time ()> $ smstime) & preg_match ("/1 [3458] {1} \ d {9} $/", $ tel )) {$ smscode = rand (1000,999 9); // send the Verification Code require LIB_PATH. 'org/Taobao-sdk-php/TopSdk. php '; $ c = new TopClient; $ c-> appkey = '000000'; // The original 23307560 $ c-> secretKey = '21ef24dd4c51e20693c5db0983c433e7 '; // original 0402169f466d8fed780e7f07edd25177 $ req = new AlibabaAliqinFcSmsNumSendRequest; $ req-> setSmsType ("normal"); $ req-> setSmsFreeS IgnName ("registration verification"); $ req-> setSmsParam ('{"code ":"'. $ smscode. '"," product ":" [duozhu dianbao] "}'); $ req-> setRecNum (" {$ tel }"); $ req-> setSmsTemplateCode ("SMS_5056863"); $ resp = $ c-> execute ($ req); if (! $ Resp-> code) {// set the sending limit time session ($ this-> smstime, time () + 50 ); // set the verification code to a valid session within 5 minutes ($ this-> smscode, array ('code' => $ smscode, 'time' => time () + 600 ));} else {// write log File $ log = date ('Y-m-d H: I: s '). "failed to send sub_code: {$ resp-> sub_code} sub_msg: {$ resp-> sub_msg }". PHP_EOL; file_put_contents (RUNTIME_PATH. 'Log/smscode. log', $ log, FILE_APPEND);} $ this-> ajaxReturn (array ('result' =>! $ Resp-> code);} else {$ this-> ajaxReturn (array ('result' => false, 'error' => 'invalid request');} // send the change password verification code public function sendSmsexcode () {session ($ this-> smstime, null ); $ smstime = session ($ this-> smstime); $ tel = $ this-> _ post ('tel', 'trim'); if (IS_AJAX &&(! $ Smstime | time ()> $ smstime) & preg_match ("/1 [3458] {1} \ d {9} $/", $ tel )) {$ smscode = rand (1000,999 9); // send the Verification Code require LIB_PATH. 'org/Taobao-sdk-php/TopSdk. php '; $ c = new TopClient; $ c-> appkey = '000000'; // The original 23307560 $ c-> secretKey = '21ef24dd4c51e20693c5db0983c433e7 '; // original 0402169f466d8fed780e7f07edd25177 $ req = new AlibabaAliqinFcSmsNumSendRequest; $ req-> setSmsType ("normal"); $ req-> setSmsFreeS IgnName ("Change Verification"); // The text message signature is fixed and cannot be changed to another word $ req-> setSmsParam ('{"code ":"'. $ smscode. '"," product ":" [duozhu dianbao] "}'); $ req-> setRecNum (" {$ tel }"); $ req-> setSmsTemplateCode ("SMS_5056861"); $ resp = $ c-> execute ($ req); if (! $ Resp-> code) {// set the sending limit time session ($ this-> smstime, time () + 50 ); // set the verification code to a valid session within 5 minutes ($ this-> smscode, array ('code' => $ smscode, 'time' => time () + 600 ));} else {// write log File $ log = date ('Y-m-d H: I: s '). "failed to send sub_code: {$ resp-> sub_code} sub_msg: {$ resp-> sub_msg }". PHP_EOL; file_put_contents (RUNTIME_PATH. 'Log/smscode. log', $ log, FILE_APPEND);} $ this-> ajaxReturn (array ('result' =>! $ Resp-> code);} else {$ this-> ajaxReturn (array ('result' => false, 'error' => 'invalid request '));}}

Summary:

1. Text message verification is used for registration and password modification.
2. For security, front-end ajax verification. The backend is also verified.
3. The process is reasonable and easy to switch.
4. Comprehensive Functions, logon, registration, and password modification.

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.