PHP+Ajax邮箱找回密码,发送临时链接更新密码用
如果没有账号,可以先到这里注册《PHP激活用户注册验证邮箱》
XML/HTML Code
- <div class="demo">
- <p>用户可以通过邮箱找回密码</p>
- <p><strong>输入您注册的电子邮箱,找回密码:</strong></p>
- <p style="margin:20px 0"><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>
- </div>
JavaScript Code
- <script type="text/javascript">
- $(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>");
- }
- });
- }
- });
- })
- </script>
sendmail.php
PHP Code
- require('../../conn.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.freejs.net/demo/556/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;
- }
- function sendmail($time, $email, $url) {
- require 'class.phpmailer.php';
- try {
- $smtpserver ="smtp.163.com";
- $emailaddress= "";
- $emailpassword= "";
- $email_address= $email;
- $mail = new PHPMailer(true); //New instance, with exceptions enabled
- $body="亲爱的" . $email . ":<br/>您在" . $time . "提交了找回密码请求。请点击下面的链接重置密码(按钮24小时内有效)。<br/><a href='" . $url . "' target='_blank'>" . $url . "</a><br/>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问。<br/>如果您没有提交找回密码请求,请忽略此邮件。";
- $body = preg_replace('/\\/','', $body); //Strip backslashes
- $mail->IsSMTP(); // tell the class to use SMTP
- $mail->SMTPAuth = true; // enable SMTP authentication
- $mail->Port = 25; // set the SMTP server port
- $mail->Host = $smtpserver; // SMTP server
- $mail->Username = $emailaddress; // SMTP server username
- $mail->Password = $emailpassword; // SMTP server password
- $mail->CharSet = "utf-8"; //设置字符集编码
- $mail->IsSendmail(); // tell the class to use Sendmail
- //如果配置了sendmail这里就用sendmail,本地测试把这个注释掉就可以了
- $mail->AddReplyTo($emailaddress,"freejs.net");
- $mail->From = $emailaddress;
- $mail->FromName = "freejs.net";
- $to = $email_address;
- $mail->AddAddress($to);
- $mail->Subject = "找回密码";
- $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
- $mail->WordWrap = 80; // set word wrap
- $mail->MsgHTML($body);
- $mail->IsHTML(true); // send as HTML
- $mail->Send();
- echo "<div class='sendmail_ok'>恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号</div>";
- } catch (phpmailerException $e) {
- echo $e->errorMessage();
- }
- //发送邮件结束
- }
- 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;
- }
- }
原文地址:http://www.freejs.net/article_biaodan_556.html