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:
-
- $(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 email address!");
- }else{
- $("#sub_btn").attr("disabled","disabled").val ('Submitting..').css("cursor","default");
- $.post("sendmail.php",{mail:email},function(msg){
- if(msg=="noreg "){
- $("#chkmsg").html("This email address has not been registered yet!");
- $("#sub_btn").removeAttr("disabled").val('Submit').css("cursor ","pointer");
- }else{
- $(".demo").html("
"+msg+"");
- }
- });
- }
- }) ;
- })
-
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:
-
- include_once("connect.php");//Connect to the 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){ //This email address has not been registered yet!
- echo 'noreg';
- exit;
- }else{
- $row = mysql_fetch_array($query);
- $getpasstime = time();
- $uid = $row['id'];
- $token = md5($ uid.$row['username'].$row['password']);//Combined 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){//The email was sent successfully
- $msg = 'The system has sent an email to your mailbox
Please log in to your mailbox to reset your password in time! ';
- //Update data sending time
- mysql_query("update `t_user` set `getpasstime`='$getpasstime' where id='$uid '");
- }else{
- $msg = $result;
- }
- echo $msg;
- }
-
- //Send email
- function sendmail($time,$email,$url){
- include_once("smtp.class.php");
- $smtpserver = ""; //SMTP server, Such as smtp.163.com
- $smtpserverport = 25; //SMTP server port
- $smtpusermail = ""; //SMTP server user email
- $smtpuser = ""; //SMTP server user account
- $smtppass = " "; //The user password of the SMTP server
- $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
- //A true here means that authentication is used, otherwise authentication is not used .
- $emailtype = "HTML"; //Email type, text: text; Web page: HTML
- $smtpemailto = $email;
- $smtpemailfrom = $smtpusermail;
- $emailsubject = "jbxue.com - Retrieve Password";
- $emailbody = "Dear".$email.":
You submitted a password retrieval request at ".$time.". Please click 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 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:
-
- include_once("connect.php");//Connect to the 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 = 'This link has expired! ';
- }else{
- //Reset password...
- $msg = 'Please reset your password and display the reset password form,
This is just a demonstration, skip it. ';
- }
- }else{
- $msg = 'Invalid link';
- }
- }else{
- $msg = 'Bad link! ';
- }
- 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:
-
- CREATE TABLE `t_user` (
- `id` int(11) NOT NULL auto_increment,
- `username` varchar(30) NOT NULL,
- `password` varchar(32) NOT NULL,
- `email` varchar(50) NOT NULL,
- `getpasstime` int(10) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Copy code
|