PHP uses jQuery to retrieve passwords

Source: Internet
Author: User
Tags preg
This article will use PHP + Mysql + jQuery to implement a password retrieval function and share with you ideas and specific code. For more information, see.

This article will use PHP + Mysql + jQuery to implement a password retrieval function and share with you ideas and specific code. For more information, see.

Generally, the password retrieval function does not actually retrieve forgotten passwords because our passwords are encrypted and saved, generally, developers generate a new password or a specific link and send an email to the user's mailbox after verifying the user information, the Reset Password module of the user's email link to the website resets the new password.

Of course, some websites also use text messages to retrieve passwords. The principle is to verify the identity by sending a verification code, just like sending an email for verification, in the end, you still need to reset the password to complete the password retrieval process.

The general steps are as follows:

1. Enter the email address for registration in the form;
2. Verify that the user email address is correct. If the user email address does not exist in the User table of the website, the system prompts that the user email address is not registered;
3. Send an email. If the user's email address does exist in the User table, combine the string used to verify the user information and construct a URL to send it to the user's email address;
4. the user logs on to the mailbox and receives an email. Click the URL to link to the website verification program;
5. The website program queries the local user table through the user request string to check whether the user information is correct;
6. If the password is correct, go to the Reset Password page and reset the new password. Otherwise, the system prompts that the user verification is invalid.

HTML

On the password retrieval page, we place an email that requires the user to enter the account used for registration, and then submit the front-end js to process the interaction.

Enter your registered email address and retrieve your password:

JQuery

After you enter your email address and click Submit, jQuery first verifies that the email address format is correct. If yes, it sends the email address to the backend. php sends Ajax requests, sendmail. php is responsible for verifying whether the mailbox exists and sending emails, and returning the corresponding processing results to the front-end page. Please refer to the jQuery code:

$ (Function () {$ ("# sub_btn "). click (function () {var email = $ ("# email "). val (); var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * // matches Emailif (email = ''|! Preg. test (email) {$ ("# chkmsg" pai.html ("please fill in the correct email! ");} Else {$ (" # sub_btn "). attr ("disabled", "disabled" 2.16.val('submit..'resumable .css ("cursor", "default"); $. post ("sendmail. php ", {mail: email}, function (msg) {if (msg =" noreg ") {$ (" # chkmsg "..html (" this email has not been registered! "); $ (" # Sub_btn "). removeAttr ("disabled "). val ('submit 'character .css ("cursor", "pointer");} else {$ (". demo "cmd.html (" "+ msg + "");}});}});})

The above jQuery code is convenient and concise to complete front-end interaction operations. If you have a certain jQuery Foundation, the above Code is clear and not explained.
Of course, don't forget to load the jQuery library file on the page. Some people often ask me why I downloaded the demo from jb51.net, that 80% is because jquery or other file loading paths are incorrect, leading to unnecessary file loading.

PHP

Sendmail. php needs to verify whether the Email exists in the System user table. If there is, read the user information and wake up the user ID, user name, and password with md5 encryption to generate a special string as the verification code for password retrieval, then construct the URL. In addition, in order to control the timeliness of URL links, we will record the operation time when the user submitted the password retrieval action, and finally call the mail sending class to send the mail to the user's mailbox, and send the mail class smtp. class. php has been packaged. download it.

Include_once ("connect. php "); // connect to the database $ email = stripslashes (trim ($ _ POST ['mail']); $ SQL =" select id, username, password from 't_user' where 'email '=' $ email '"; $ query = mysql_query ($ SQL); $ num = mysql_num_rows ($ query ); if ($ num = 0) {// This email address is not registered yet! Echo 'noreg '; exit;} else {$ row = mysql_fetch_array ($ query); $ getpasstime = time (); $ uid = $ row ['id']; $ token = md5 ($ uid. $ row ['username']. $ row ['Password']); // combined Verification Code $ url = "http://www.jb51.net/demo/resetpass/reset.php? Email = ". $ email. "& token = ". $ token; // construct the URL $ time = date ('Y-m-d H: I '); $ result = sendmail ($ time, $ email, $ url ); if ($ result = 1) {// The email is successfully sent $ msg = 'the system has sent an email to your mailbox
Please log on to your mailbox and reset your password in time! '; // Update the data sending time mysql_query ("update't _ user' set 'getpasstime' =' $ getpasstime 'where");} else {$ msg = $ result ;} echo $ msg;} // send email function sendmail ($ time, $ email, $ url) {include_once ("smtp. class. php "); $ smtpserver =" "; // SMTP server, such as smtp.163.com $ smtpserverport = 25; // SMTP server port $ smtpusermail = ""; // SMTP server user email $ smtpuser = ""; // SMTP server user account $ smtppass = ""; // SMTP server user password $ smtp = new Smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // here, true indicates that authentication is used, otherwise, authentication is not used. $ emailtype = "HTML"; // mail type, text: text; webpage: HTML $ smtpemailto = $ email; $ smtpemailfrom = $ smtpusermail; $ emailsubject = "jb51.net-retrieve password"; $ emailbody = "dear ". $ email. ":
You submitted a password retrieval request in ". $ time. Click the link below to reset the password (the button is valid within 24 hours ).
". $ Url." "; $ rs = $ smtp-> sendmail ($ smtpemailto, $ smtpemailfrom, $ emailsubject, $ emailbody, $ emailtype); return $ rs ;}

Now, your mailbox will receive a password retrieval email from helloweba. There is a URL link in the email. Click the link to jb51.net reset. php to verify the email address.

Include_once ("connect. php "); // connect to the database $ token = stripslashes (trim ($ _ GET ['Token']); $ email = stripslashes (trim ($ _ GET ['email ']); $ SQL = "select * from 't_ user' where email =' $ email '"; $ query = mysql_query ($ SQL); $ row = mysql_fetch_array ($ query); if ($ row) {$ mt = md5 ($ row ['id']. $ row ['username']. $ row ['Password']); if ($ mt = $ token) {if (time ()-$ row ['getpasstime']> 24*60*60) {$ msg = 'this link has expired! ';} Else {// reset the password... $ msg =' reset the password to display the password reset form,
This is just a demonstration, skipped. ';}} Else {$ msg = 'invalid link';} else {$ msg = 'incorrect link! ';} Echo $ msg;

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.