PHP, Mysql, and jQuery implementation code for password retrieval

Source: Internet
Author: User
Tags preg
PHP, Mysql, and jQuery implementation code for password retrieval

  1. Enter your registered email address and retrieve your password:

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 corresponding processing results to the front-end page.

JQuery code:

  1. $ (Function (){
  2. $ ("# Sub_btn"). click (function (){
  3. Var email = $ ("# email"). val ();
  4. Var preg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */; // Match Email
  5. If (email = ''|! Preg. test (email )){
  6. $ ("# Chkmsg" cmd.html ("Enter the correct email address! ");
  7. } Else {
  8. $ ("# Sub_btn"). attr ("disabled", "disabled" ).val('submit..'hangzhou.css ("cursor", "default ");
  9. $. Post ("sendmail. php", {mail: email}, function (msg ){
  10. If (msg = "noreg "){
  11. $ ("# Chkmsg" cmd.html ("This email has not been registered! ");
  12. $ ("# Sub_btn"). removeAttr ("disabled"). val ('submit your CSS ("cursor", "pointer ");
  13. } Else {
  14. $ (". Demo" ).html ("" + msg + "");
  15. }
  16. });
  17. }
  18. });
  19. })

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, do not forget to load the jQuery library file on the page.

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, the operation time for users to submit password retrieval operations is recorded, and finally the Mail sending class is called to send an email to the user's mailbox.

Send mail class smtp. class. php, in the source code package provided in this article.

Example:

  1. Include_once ("connect. php"); // connect to the database
  2. $ Email = stripslashes (trim ($ _ POST ['mail']);
  3. $ SQL = "select id, username, password from 't_ user' where 'email '=' $ email '";
  4. $ Query = mysql_query ($ SQL );
  5. $ Num = mysql_num_rows ($ query );
  6. If ($ num = 0) {// This email address is not registered yet!
  7. Echo 'noreg ';
  8. Exit;
  9. } Else {
  10. $ Row = mysql_fetch_array ($ query );
  11. $ Getpasstime = time ();
  12. $ Uid = $ row ['id'];
  13. $ Token = md5 ($ uid. $ row ['username']. $ row ['password']); // combined verification code
  14. $ Url = "http://bbs.it-home.org/demo/resetpass/reset.php? Email = ". $ email ."
  15. & Token = ". $ token; // Construct a URL
  16. $ Time = date ('Y-m-d H: I ');
  17. $ Result = sendmail ($ time, $ email, $ url );
  18. If ($ result = 1) {// The email is successfully sent.
  19. $ Msg = 'The system has sent an email to your mailbox
    Please log on to your mailbox and reset your password in time! ';
  20. // Update the data sending time
  21. Mysql_query ("update't _ user 'set' getpasstime '=' $ getpasstime 'where id =' $ uid '");
  22. } Else {
  23. $ Msg = $ result;
  24. }
  25. Echo $ msg;
  26. }
  27. // Send an email
  28. Function sendmail ($ time, $ email, $ url ){
  29. Include_once ("smtp. class. php ");
  30. $ Smtpserver = ""; // SMTP server, such as smtp.163.com
  31. $ Smtpserverport = 25; // SMTP server port
  32. $ Smtpusermail = ""; // user email address of the SMTP server
  33. $ Smtpuser = ""; // User account of the SMTP server
  34. $ Smtppass = ""; // SMTP server user password
  35. $ Smtp = new Smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass );
  36. // Here, "true" indicates that authentication is used; otherwise, authentication is not used.
  37. $ Emailtype = "HTML"; // mail type, text: text; webpage: HTML
  38. $ Smtpemailto = $ email;
  39. $ Smtpemailfrom = $ smtpusermail;
  40. $ Emailsubject = "jbxue.com-retrieve password ";
  41. $ Emailbody = "dear". $ email .":
    You submitted a password retrieval request in ". $ time. Click the link below to reset the password.
  42. (The button is valid within 24 hours ).
    ". $ Url ."";
  43. $ Rs = $ smtp-> sendmail ($ smtpemailto, $ smtpemailfrom, $ emailsubject, $ emailbody, $ emailtype );
  44. Return $ rs;
  45. }

At this time, the mailbox will receive a password retrieval email from jbxue, which contains a URL link, click the link to jbxue.com's reset. php to verify the mailbox.

Example:

  1. Include_once ("connect. php"); // connect to the database
  2. $ Token = stripslashes (trim ($ _ GET ['token']);
  3. $ Email = stripslashes (trim ($ _ GET ['email ']);
  4. $ SQL = "select * from't _ user' where email = '$ email '";
  5. $ Query = mysql_query ($ SQL );
  6. $ Row = mysql_fetch_array ($ query );
  7. If ($ row ){
  8. $ Mt = md5 ($ row ['id']. $ row ['username']. $ row ['password']);
  9. If ($ mt = $ token ){
  10. If (time ()-$ row ['getpasstime']> 24*60*60 ){
  11. $ Msg = 'this link has expired! ';
  12. } Else {
  13. // Reset the password...
  14. $ Msg = 'set the password again. The password reset form is displayed,
    This is just a demonstration, skipped. ';
  15. }
  16. } Else {
  17. $ Msg = 'invalid link ';
  18. }
  19. } Else {
  20. $ Msg = 'invalid link! ';
  21. }
  22. 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.

T_user structure of the data table:

  1. Create table 't_ user '(
  2. 'Id' int (11) not null auto_increment,
  3. 'Username' varchar (30) not null,
  4. 'Password' varchar (32) not null,
  5. 'Email 'varchar (50) not null,
  6. 'Getpasstime' int (10) not null,
  7. Primary key ('id ')
  8. ) ENGINE = MyISAM default charset = utf8;

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.