PHP combined with jquery to retrieve the password, phpjquery retrieve password _php tutorial

Source: Internet
Author: User
Tags md5 encryption preg jquery library

PHP combined with jquery to retrieve the password, phpjquery retrieve the password


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.

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.

Html

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.

Enter your registered email address to retrieve your password:

Jquery

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 ("

"+msg+"

"); } }); }

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 Jb51.net demo how to use, that 80% is jquery or other file loading path is wrong cause the necessary files are not loaded.

Php

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, will record the user to submit the operation time to retrieve the password action, the last call to Send mail class mail to the user mailbox, send the message Class smtp.class.php has been packaged, please download.

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.bkjia.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 = ' 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 = ""; 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 = "jb51.net-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; }

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 jb51.net 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 your password, display the
Here is just a demo, skip over. '; } } else{ $msg = ' invalid link '; }} else{ $msg = ' wrong link! ';

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) is not NULL,  

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1036258.html www.bkjia.com true http://www.bkjia.com/PHPjc/1036258.html techarticle PHP combined with jquery to realize the password, phpjquery recover password often said the password back function is not really able to forget the password back, because our password is encrypted saved, generally open ...

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