PHP, Mysql, jquery retrieve password implementation code

Source: Internet
Author: User
    1. Enter your registered email address to retrieve your password:

Copy Code

When the user enters the mailbox and clicks Submit, jquery verifies that the mailbox format is correct, and if it is correct, sends an AJAX request to the background sendmail.php, sendmail.php is responsible for verifying that the mailbox exists and sending the message, and returns the corresponding processing results to the foreground 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"). HTML ("Please fill in the correct mailbox!") ");
    7. }else{
    8. $ ("#sub_btn"). attr ("Disabled", "disabled"). Val (' Submit: '). CSS ("cursor", "Default");
    9. $.post ("sendmail.php", {mail:email},function (msg) {
    10. if (msg== "Noreg") {
    11. $ ("#chkmsg"). HTML ("The mailbox is not registered! ");
    12. $ ("#sub_btn"). Removeattr ("Disabled"). Val (' Submit '). CSS ("cursor", "pointer");
    13. }else{
    14. $ (". Demo"). HTML ("

      "+msg+"

      ");
    15. }
    16. });
    17. }
    18. });
    19. })
Copy Code

The jquery code used above is convenient and concise to complete the front-end interaction, if you have a certain jquery basis, the above code at a glance, not much explanation. Of course, don't forget to load the jquery library file in the page.

sendmail.php need to verify that the e-mail exists in the System User table, if there is, read the user information, the user ID, user name and password awakened MD5 encryption to generate a special string as the code to retrieve the password, and then construct the URL. At the same time, in order to control the timeliness of the URL link, the user submits the operation time to retrieve the password action, and finally call the Mail send class to send mail to the user mailbox.

Send the Message Class smtp.class.php in the source package provided in this document.

Example:

  1. Include_once ("connect.php");//Connect 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) {//The mailbox has not been registered!
  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 ']);//Combination Verification code
  14. $url = "http://bbs.it-home.org/demo/resetpass/reset.php?email=". $email. "
  15. &token= ". $token;//Construct URL
  16. $time = Date (' y-m-d h:i ');
  17. $result = SendMail ($time, $email, $url);
  18. if ($result ==1) {//mail sent successfully
  19. $msg = ' The system has sent a message to your mailbox
    Please login to your email address to reset your password in time! ';
  20. Update Data Send 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 mail
  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 mailboxes for the SMTP server
  33. $smtpuser = ""; User account for the SMTP server
  34. $smtppass = ""; User password for SMTP server
  35. $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass);
  36. A true in this case is that authentication is used, otherwise it is not used.
  37. $emailtype = "HTML"; Letter type, text: text; Web page: HTML
  38. $smtpemailto = $email;
  39. $smtpemailfrom = $smtpusermail;
  40. $emailsubject = "jbxue.com-Recover password";
  41. $emailbody = "Dear". $email. " :
    You are in the ". $time." A request to retrieve the password was submitted. Please click on the link below to reset your password
  42. (the button is valid within 24 hours).
    ". $url.";
  43. $rs = $smtp->sendmail ($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
  44. return $rs;
  45. }
Copy Code

At this time, the mailbox will receive a password to retrieve the message from Jbxue, the message content has a URL link, click the link to jbxue.com reset.php to verify the mailbox.

Example:

    1. Include_once ("connect.php");//Connect 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 = ' The link has expired! ';
    12. }else{
    13. Reset Password ...
    14. $msg = ' Reset your password, display the
      Here is just a demo, skip over. ';
    15. }
    16. }else{
    17. $msg = ' Invalid link ';
    18. }
    19. }else{
    20. $msg = ' wrong link! ';
    21. }
    22. Echo $msg;
Copy Code

reset.php first accept the parameter email and token, and then according to the email Query data table T_user whether there is an email, if there is to obtain the user's information, The token value is built just like the token combination in sendmail.php, and then compared to the token passed by the URL, if the current time differs from sending the message for more than 24 hours, the link is expired! ", instead, the link is valid, and turned to the Reset Password page, and finally the user set a new password.

Summary: Through the registration of email verification and this article to retrieve the password, we know the application of e-mail in the development of the website and its importance, of course, now also popular SMS verification application, the need for the relevant SMS interface docking on it.

Data Sheet T_user Structure:

    1. CREATE TABLE ' T_user ' (
    2. ' id ' int (one) not NULL auto_increment,
    3. ' username ' varchar (+) not NULL,
    4. ' Password ' varchar (+) not NULL,
    5. ' Email ' varchar (not NULL),
    6. ' Getpasstime ' int (ten) is not NULL,
    7. PRIMARY KEY (' id ')
    8. ) Engine=myisam DEFAULT Charset=utf8;
Copy Code
  • 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.