PHP uses jQuery to retrieve passwords. phpjquery to retrieve passwords _ PHP Tutorial

Source: Internet
Author: User
Tags preg
PHP uses jQuery to retrieve the password and phpjquery to retrieve the password. PHP uses jQuery to retrieve the password. phpjquery's password retrieval function is usually called the password retrieval function, which does not actually retrieve the forgotten password because our password is encrypted and saved, generally, PHP and jQuery are used to retrieve the password, while phpjquery is used to retrieve the password.

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 +) * // match the Email if (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.bkjia.com/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 id =' $ uid '");} 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;

Reset. php first accepts the parameter email and token, and then queries whether the email exists in the t_user data table based on the Email. if so, it obtains the information of the user and sendmail. in php, the token combination method is the same as the token value, and then compare it with the token passed in the url. if the current time differs from the time when the email is sent by more than 24 hours, the message "This link has expired!" is displayed! ", Otherwise, the link is valid, and is transferred to the password reset page. Finally, the user sets the new password.

Conclusion: through email registration verification and password retrieval in this article, we know the application of email sending in website development and its importance. of course, text message verification is also popular now, this requires the connection of related SMS interfaces.
Finally, the t_user structure of the data table is attached:

 CREATE TABLE `t_user` (  `id` int(11) NOT NULL auto_increment,  `username` varchar(30) NOT NULL,  `password` varchar(32) NOT NULL,  `email` varchar(50) NOT NULL,  `getpasstime` int(10) NOT NULL,  PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

The above is all the content of this article. I hope you will like it.

The password retrieval function of the KeyStore is generally not used to retrieve forgotten passwords, because our passwords are encrypted and saved...

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.