首先是 index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>Document</title> </head> <body><h2>找回密码</h2><p class="demo"></p><form action=""><table><tr><td>用户名:</td><td><input type="text" id="username"/></td></tr><tr><td>邮箱:</td><td><input type="text" class="input" name="email" id="email"><span id="chkmsg"></span></td></tr><tr><td><input type="button" class="btn" id="sub_btn" value="提 交"></td><td></td></tr></table></form> </body> </html> <script src="jq.js"></script> <script>$(function(){$("#sub_btn").click(function(){var username=$("#username").val();var email = $("#email").val();var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Emailif(email=='' || !preg.test(email)){$("#chkmsg").html("请填写正确的邮箱!");}else{$("#sub_btn").attr("disabled","disabled").val('提交中..').css("cursor","default");$.post("sendmail.php",{mail:email,username:username},function(msg){ // if(msg=="noreg"){ // $("#chkmsg").html("该邮箱尚未注册!"); // $("#sub_btn").removeAttr("disabled").val('提 交').css("cursor","pointer"); // }else{$(".demo").html("<h3>"+msg+"</h3>"); // }});}});}) </script>然后 sendmail.php
reset.php<?php include_once("connect.php");//连接数据库 include_once('class.phpmailer.php'); $username=$_POST['username']; $email = stripslashes(trim($_POST['mail']));$sql = "select uid,username,pwd from `user` where `username`='$username'"; $re = $pdo->query($sql);$row =$re->fetch(PDO::FETCH_ASSOC);$getpasstime = time();$uid = $row['uid'];$token = md5($uid.$row['username'].$row['pwd']);//组合验证码$url = "http://localhost/xiaoer/zhoukao1/reset.php?email=".$email."&token=".$token;//构造URL$time = date('Y-m-d H:i');//邮箱标题$emailsubject = "找回密码"; //拼接邮箱内容$emailbody= "亲爱的".$email.":<br/>您在".$time."提交了修改密码请求。请点击下面的链接重置密码(按钮24小时内有效)。<br/><a href='".$url."'target='_blank'>".$url."</a>";$result = sendmail($emailsubject,$emailbody,$username,$email);if($result==1){//邮件发送成功$msg = '系统已向您的邮箱发送了一封邮件<br/>请登录到您的邮箱及时重置您的密码!';//更新数据发送时间$pdo->query("update `user` set `getpasstime`='$time' where uid='$uid '");}else{$msg = $result;}echo $msg; //}//发送邮件 function sendmail($emailsubject,$emailbody,$username,$email){$mail= new PHPMailer();/*服务器相关信息*/// $mail->SmtpMail.SmtpServer = "127.0.0.1";$mail->IsSMTP(); //设置使用SMTP服务器发送$mail->SMTPAuth = true; //开启SMTP认证$mail->Host = 'smtp.163.com'; //设置 SMTP 服务器,自己注册邮箱服务器地址$mail->Username = ''; //发信人的邮箱名称$mail->Password = ''; //发信人的邮箱密码/*内容信息*/$mail->IsHTML(true); //指定邮件格式为:html$mail->CharSet ="UTF-8"; //编码$mail->From = ''; //发件人完整的邮箱名称$mail->FromName = $username; //发信人署名$mail->Subject = $emailsubject; //信的标题$mail->MsgHTML($emailbody); //发信主体内容//$mail->AddAttachment("15.jpg"); //附件/*发送邮件*/$mail->AddAddress($email); //收件人地址//使用send函数进行发送if($mail->Send()) {return true;} else {self::$error=$mail->ErrorInfo;return false;} }?>
<?php header("content-type:text/html;charset=utf-8"); include_once("connect.php");//连接数据库 $token = stripslashes(trim($_GET['token'])); $email = stripslashes(trim($_GET['email'])); $sql = "select * from `user` where email='$email'";$query = $pdo->query($sql); $row =$query->fetch(PDO::FETCH_ASSOC);if($row){$pwd=$row['pwd'];$mt = md5($row['uid'].$row['username'].$row['pwd']);if($mt==$token){$mydate=strtotime($row['getpasstime']);if(time()-$mydate>24*60*60){$msg = '该链接已过期!';}else{//重置密码...此处略过//$msg =include_once('update.php');}}else{$msg = '无效的链接';} }else{$msg = '错误的链接!'; } echo $msg; ?>