Enter your registered email address to retrieve your password:
Copy CodeWhen 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:
- $ (function () {
- $ ("#sub_btn"). Click (function () {
- var email = $ ("#email"). Val ();
- var preg =/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */; Match Email
- if (email== ' | | |!preg.test (email)) {
- $ ("#chkmsg"). HTML ("Please fill in the correct mailbox!") ");
- }else{
- $ ("#sub_btn"). attr ("Disabled", "disabled"). Val (' Submit: '). CSS ("cursor", "Default");
- $.post ("sendmail.php", {mail:email},function (msg) {
- if (msg== "Noreg") {
- $ ("#chkmsg"). HTML ("The mailbox is not registered! ");
- $ ("#sub_btn"). Removeattr ("Disabled"). Val (' Submit '). CSS ("cursor", "pointer");
- }else{
- $ (". Demo"). HTML ("
"+msg+" ");
- }
- });
- }
- });
- })
Copy CodeThe 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:
- Include_once ("connect.php");//Connect 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) {//The mailbox has not been registered!
- Echo ' Noreg ';
- Exit
- }else{
- $row = Mysql_fetch_array ($query);
- $getpasstime = time ();
- $uid = $row [' id '];
- $token = MD5 ($uid. $row [' username ']. $row [' password ']);//Combination Verification code
- $url = "http://bbs.it-home.org/demo/resetpass/reset.php?email=". $email. "
- &token= ". $token;//Construct URL
- $time = Date (' y-m-d h:i ');
- $result = SendMail ($time, $email, $url);
- if ($result ==1) {//mail sent successfully
- $msg = ' The system has sent a message to your mailbox
Please login to your email address to reset your password in time! ';
- Update Data Send time
- mysql_query ("Update ' T_user ' set ' getpasstime ' = ' $getpasstime ' where id= ' $uid '");
- }else{
- $msg = $result;
- }
- Echo $msg;
- }
- Send mail
- function SendMail ($time, $email, $url) {
- Include_once ("smtp.class.php");
- $smtpserver = ""; SMTP server, such as smtp.163.com
- $smtpserverport = 25; SMTP Server port
- $smtpusermail = ""; User mailboxes for the SMTP server
- $smtpuser = ""; User account for the SMTP server
- $smtppass = ""; User password for SMTP server
- $SMTP = new SMTP ($smtpserver, $smtpserverport, True, $smtpuser, $smtppass);
- A true in this case is that authentication is used, otherwise it is not used.
- $emailtype = "HTML"; Letter type, text: text; Web page: HTML
- $smtpemailto = $email;
- $smtpemailfrom = $smtpusermail;
- $emailsubject = "jbxue.com-Recover password";
- $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
- (the button is valid within 24 hours).
". $url.";
- $rs = $smtp->sendmail ($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
- return $rs;
- }
Copy CodeAt 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:
- Include_once ("connect.php");//Connect 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 = ' The link has expired! ';
- }else{
- Reset Password ...
- $msg = ' Reset your password, display the
Here is just a demo, skip over. ';
- }
- }else{
- $msg = ' Invalid link ';
- }
- }else{
- $msg = ' wrong link! ';
- }
- Echo $msg;
Copy Codereset.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:
- CREATE TABLE ' T_user ' (
- ' id ' int (one) not NULL auto_increment,
- ' username ' varchar (+) not NULL,
- ' Password ' varchar (+) not NULL,
- ' Email ' varchar (not NULL),
- ' Getpasstime ' int (ten) is not NULL,
- PRIMARY KEY (' id ')
- ) Engine=myisam DEFAULT Charset=utf8;
Copy Code |