(Advanced article) Php+mysql+jquery Retrieve password

Source: Internet
Author: User
Tags md5 encryption preg jquery library
The code is as follows:

Usually the password retrieval function is not really able to retrieve the forgotten password, because our password is encrypted, the general developer will verify the user information through the program to generate a new password or generate a specific link and send mail to the user's mailbox, the user from the mailbox link to the site's Reset password module re-set the new password.

Of course, some websites also have mobile phone message to retrieve the password, the principle is to send a verification code to Yue Heyue, and send email verification, and ultimately to reset the password to complete the process of retrieving the password.

This article will use Php+mysql+jquery to achieve a password recovery function, the general steps are:

1. Form enter the mailbox when registering;

2. Verify that the user mailbox is correct, if the user mailbox does not exist in the user table of the site, then prompt the user mailbox is not registered;

3. Send the message, if the user mailbox does exist in the user table, then combine the string used to validate the user information and construct the URL to send to the user's mailbox;

4. User login mailbox to receive mail, click the URL link to the website verification program;

5. The website program queries the local user table through the string requested by the user, which is more accurate than the user information;

6. If correct, go to the Reset Password page and reset the new password, otherwise the user is prompted to verify that it is invalid.


We place a password on the page to ask the user to enter the registration of the mailbox, and then submit the foreground JS to handle the interaction.

<p><strong> Enter your registered email address, retrieve your password:</strong></p> <p><input type= "text" class= "input" name = "Email" id= "email" ><span id= "chkmsg" ></span></p> <p><input type= "button" class= "BTN" Id= "SUB_BTN" value= "Submit" ></p>

When the user finishes entering the mailbox and clicks Submit, jquery verifies that the mailbox is formatted correctly. If correct, by sending an AJAX request to the background sendmail.php, sendmail.php is responsible for verifying that the mailbox exists and sending the message, and returning the corresponding processing results to the foreground page, see the 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 ("

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, do not forget to load the jquery library files in the page, some students often ask me to download from the Helloweba.com demo how to use, that 80% is jquery or other file loading path is wrong cause the necessary files are not loaded.

The

sendmail.php needs to verify that the email exists in the System User table, and if so, reads the user information, wakes up the user ID, username, and password MD5 encryption generates a special string as the verification code to retrieve the password, and constructs the URL. At the same time, in order to control the timeliness of the URL link, the user will be logged to submit the operation time to retrieve password action, and finally call the Mail send class to send mail to the user mailbox, send the message

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://www.helloweba.com/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) {//message sent successfully $msg = ' system has sent an email to your email <br/> 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 = ""; The user mailbox of the SMTP server $smtpuser = ""; The user account for the SMTP server $smtppass = "";      The user password for the 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 = "helloweba.com-Recover password"; $emailbody = "Dear". $email. " :<br/> you 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). <br/><a href= ' ". $url." target= ' _blank ' > ". $url."     </a> ";      $rs = $smtp->sendmail ($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); return $rs; }

OK, this time your mailbox will receive a password to retrieve the mail from Helloweba, the message content has a URL link, click the link to helloweba.com reset.php to verify the mailbox.

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 password, show resetting password form,<br/> Here is just a demo, skip. ';         }     } else{         $msg =  ' Invalid link ';     }} else{     $msg =  ' wrong link! ';     } Echo $msg;

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.

Finally, attach the data sheet T_user structure:

CREATE TABLE ' t_user ' (   ' id ' int (one) not null auto_increment,   ' username ' varchar (+) ' NOT null,   ' password ' var char (+) NOT NULL,   ' email ' varchar (+) NOT NULL,   ' getpasstime ' int (ten) not NULL,   PRIMARY KEY  (' id ')) ENG Ine=myisam  DEFAULT Charset=utf8;

Above is (Advanced article) php+mysql+jquery Recover password content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.