HTMLphp
<p><strong>輸入您註冊的電子郵箱,找回密碼:</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="提 交"></p>
jQuerycss
當用戶輸入完郵箱並點擊提交後,jQuery先驗證郵箱格式是否正確,若是正確則經過向後臺sendmail.php發送Ajax請求,sendmail.php負責驗證郵箱是否存在和發送郵件,並會返回相應的處理結果給前臺頁面,請看jQuery代碼:html
$(function(){ $("#sub_btn").click(function(){ var email = $("#email").val(); var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email if(email=='' || !preg.test(email)){ $("#chkmsg").html("請填寫正確的郵箱!"); }else{ $("#sub_btn").attr("disabled","disabled").val('提交中..').css("cursor","default"); $.post("sendmail.php",{mail:email},function(msg){ if(msg=="noreg"){ $("#chkmsg").html("該郵箱還沒有註冊!"); $("#sub_btn").removeAttr("disabled").val('提 交').css("cursor","pointer"); }else{ $(".demo").html("<h3>"+msg+"</h3>"); } }); } }); })
PHPmysql
sendmail.php須要驗證Email是否存在系統用戶表中,若是有,則讀取用戶信息,將用戶id、用戶名和密碼驚醒md5加密生成一個特別的字符串做爲找回密碼的驗證碼,而後構造URL。同時咱們爲了控制URL連接的時效性,將記錄用戶提交找回密碼動做的操做時間,最後調用郵件發送類發送郵件到用戶郵箱,發送郵件類smtp.class.phpjquery
include_once("connect.php"); $email = injectChk(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) {//該郵箱還沒有註冊! echo 'noreg'; exit; } else { $row = mysql_fetch_array($query); $getpasstime = time(); $uid = $row['id']; $token = md5($uid . $row['username'] . $row['password']); $url = "http://www.erdangjiade.com/jquery/3/326/demo/reset.php?email=" . $email . "&token=" . $token; $time = date('Y-m-d H:i'); $result = sendmail($time, $email, $url); if ($result == 1) {//郵件發送成功 $msg = '系統已向您的郵箱發送了一封郵件<br/>請登陸到您的郵箱及時重置您的密碼!'; //更新數據發送時間 mysql_query("update `t_user` set `getpasstime`='$getpasstime' where id='$uid '"); } else { $msg = $result; } echo $msg; }
相關函數sql
function sendmail($time, $email, $url) { include_once("smtp.class.php"); $smtpserver = "smtp.163.com"; //SMTP服務器 $smtpserverport = 25; //SMTP服務器端口 $smtpusermail = "hjl416148489_4@163.com"; //SMTP服務器的用戶郵箱 $smtpuser = "hjl416148489_4@163.com"; //SMTP服務器的用戶賬號 $smtppass = "hjl7233163"; //SMTP服務器的用戶密碼 $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //這裏面的一個true是表示使用身份驗證,不然不使用身份驗證. $emailtype = "HTML"; //信件類型,文本:text;網頁:HTML $smtpemailto = $email; $smtpemailfrom = $smtpusermail; $emailsubject = "素材火erdangjiade.com - 找回密碼"; $emailbody = "親愛的" . $email . ":<br/>您在" . $time . "提交了找回密碼請求。請點擊下面的連接重置密碼(按鈕24小時內有效)。<br/><a href='" . $url . "' target='_blank'>" . $url . "</a><br/>若是以上連接沒法點擊,請將它複製到你的瀏覽器地址欄中進入訪問。<br/>若是您沒有提交找回密碼請求,請忽略此郵件。"; $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); return $rs; } function injectChk($sql_str) { //防止注入 $check = eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str); if ($check) { echo('非法字符串'); exit(); } else { return $sql_str; } }
reset.php重置密碼瀏覽器
include_once("connect.php"); $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 = '該連接已過時!'; }else{ //重置密碼... $msg = '請從新設置密碼,顯示重置密碼錶單,<br/>這裏只是演示,略過。'; } }else{ $msg = '無效的連接<br/>'.$mt.'<br/>'.$token; } }else{ $msg = '錯誤的連接!'; } echo $msg;