Implementation code for retrieving passwords using PHP, Mysql, and jQuery

WBOY
Release: 2016-07-25 08:55:30
Original
1036 people have browsed it
  1. Enter your registered email address to retrieve your password:

Copy code

After the user enters the email address and clicks submit, jQuery first verifies whether the email format is correct. If it is correct, it passes the Sendmail.php in the background sends Ajax requests. sendmail.php is responsible for verifying whether the mailbox exists and sending emails, and will return the 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").html("Please fill in the correct email address!");
  7. }else{
  8. $("#sub_btn").attr("disabled","disabled").val ('Submitting..').css("cursor","default");
  9. $.post("sendmail.php",{mail:email},function(msg){
  10. if(msg=="noreg "){
  11. $("#chkmsg").html("This email address has not been registered yet!");
  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 very convenient and concise to complete the front-end interactive operation. If you have a certain jQuery foundation, the above code is clear at a glance and does not require much explanation. Of course, don’t forget to load the jQuery library file in the page.

sendmail.php needs to verify whether the email exists in the system user table. If so, read the user information, encrypt the user ID, user name and password using md5 to generate a special string as a verification code for retrieving the password, and then construct the URL . At the same time, in order to control the timeliness of the URL link, the operation time of the user submitting the password retrieval action will be recorded, and finally the email sending class will be called to send the email to the user's mailbox.

Send email 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 has not been 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 URL
  16. $time = date('Y-m-d H:i');
  17. $result = sendmail($time,$email,$url);
  18. if($result==1){//The email was sent successfully
  19. $msg = 'The system has sent an email to your mailbox
    Please log in to your mailbox to reset your password in time! ';
  20. //Update 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 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 = ""; //SMTP server user email
  33. $smtpuser = ""; //SMTP server user account
  34. $smtppass = " "; //The user password of the SMTP server
  35. $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
  36. //A true here means that authentication is used, otherwise authentication is not used .
  37. $emailtype = "HTML"; //Email type, text: text; Web page: HTML
  38. $smtpemailto = $email;
  39. $smtpemailfrom = $smtpusermail;
  40. $emailsubject = "jbxue.com - Retrieve Password";
  41. $emailbody = "Dear".$email.":
    You submitted a password retrieval request at ".$time.". Please click 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, your mailbox will receive a password retrieval email from jbxue. There is a URL link in the email content. Click the link to reset.php of jbxue.com to verify the email.

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 password...
  14. $msg = 'Please reset your password and display the reset password form,
    This is just a demonstration, skip it. ';
  15. }
  16. }else{
  17. $msg = 'Invalid link';
  18. }
  19. }else{
  20. $msg = 'Bad link! ';
  21. }
  22. echo $msg;
Copy code

reset.php first accepts the parameters email and token, and then queries whether the email exists in the data table t_user based on the email. If it exists, obtain the user's information, and construct the token value in the same way as the token combination in sendmail.php, and then with Compare the token passed by the URL. If the difference between the current time and the time when the email is sent is more than 24 hours, it will prompt "The link has expired!". Otherwise, it means that the link is valid, and it will be transferred to the reset password page. Finally, The user has set a new password himself.

Summary: Through registered email verification and password retrieval via email in this article, we know the application of sending emails in website development and its importance. Of course, SMS verification applications are also popular now, and this requires related SMS interface docking.

Data table t_user structure:

  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;
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!