文章来自:博客
http://www.jb51.net/article/91944.htm
设置思路
1、用户注册时需要提供一个E-MAIL邮箱,目的就是用该邮箱找回密码。
2、当用户忘记密码或用户名时,点击登录页面的“找回密码”超链接,打开表单,并输入注册用的E-MAIL邮箱,提交。
3、系统通过该邮箱,从数据库中查找到该用户信息,并更新该用户的密码为一个临时密码(比如:12345678)。
4、系统借助Jmail功能把该用户的信息发送到该用户的邮箱中(内容包括:用户名、临时密码、提醒用户及时修改临时密码的提示语)。
5、用户用临时密码即可登录。
HTML
我们在找回密码的页面上放置一个要求用户输入注册时所用的邮箱,然后提交前台js来处理交互。
代码如下
1 <p><strong>输入您注册的电子邮箱,找回密码:</strong></p> 2 <p><input type="text" class="input" name="email" id="email"><span id="chkmsg"></span></p> 3 <p><input type="button" class="btn" id="sub_btn" value="提 交"></p>
jQuery
当用户输入完邮箱并点击提交后,jQuery先验证邮箱格式是否正确,如果正确则通过向后台sendmail.php发送Ajax请求,sendmail.php负责验证邮箱是否存在和发送邮件,并会返回相应的处理结果给前台页面,请看jQuery代码:
代码如下
1 $(function(){ 2 $("#sub_btn").click(function(){ 3 var email = $("#email").val(); 4 var preg = /^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/; //匹配Email 5 if(email=='' || !preg.test(email)){ 6 $("#chkmsg").html("请填写正确的邮箱!"); 7 }else{ 8 $("#sub_btn").attr("disabled","disabled").val('提交中..').css("cursor","default"); 9 $.post("sendmail.php",{ mail:email},function(msg){ 10 if(msg=="noreg"){ 11 $("#chkmsg").html("该邮箱尚未注册!"); 12 $("#sub_btn").removeAttr("disabled").val('提 交').css("cursor","pointer"); 13 }else{ 14 $(".demo").html("<h3>"+msg+"</h3>"); 15 } 16 }); 17 } 18 }); 19 })
以上使用的jQuery代码很方便简洁的完成了前端交互操作,如果您有一定的jQuery基础,那上面的代码一目了然,不多解释。
当然别忘了在页面中加载jQuery库文件,有的同学经常问我说从网上下载了demo怎么用不了,那80%是jquery或者其他文件加载路径错了导致没加载必要的文件。
PHP
sendmail.php需要验证Email是否存在系统用户表中,如果有,则读取用户信息,将用户id、用户名和密码惊醒md5加密生成一个特别的字符串作为找回密码的验证码,然后构造URL。同时我们为了控制URL链接的时效性,将记录用户提交找回密码动作的操作时间,最后调用邮件发送类发送邮件到用户邮箱
代码如下
1 include_once("connect.php");//连接数据库 2 3 $email = stripslashes(trim($_POST['mail'])); 4 5 $sql = "select id,username,password from `t_user` where `email`='$email'"; 6 $query = mysql_query($sql); 7 $num = mysql_num_rows($query); 8 if($num==0){ //该邮箱尚未注册! 9 echo 'noreg'; 10 exit; 11 }else{ 12 $row = mysql_fetch_array($query); 13 $getpasstime = time(); 14 $uid = $row['id']; 15 $token = md5($uid.$row['username'].$row['password']);//组合验证码 16 $url = "/demo/resetpass/reset.php?email=".$email." 17 &token=".$token;//构造URL 18 $time = date('Y-m-d H:i'); 19 $result = sendmail($time,$email,$url); 20 if($result==1){ //邮件发送成功 21 $msg = '系统已向您的邮箱发送了一封邮件<br/>请登录到您的邮箱及时重置您的密码!'; 22 //更新数据发送时间 23 mysql_query("update `t_user` set `getpasstime`='$getpasstime' where id='$uid '"); 24 }else{ 25 $msg = $result; 26 } 27 echo $msg; 28 } 29 30 //发送邮件 31 function sendmail($time,$email,$url){ 32 include_once("smtp.class.php"); 33 $smtpserver = ""; //SMTP服务器,如smtp.163.com 34 $smtpserverport = 25; //SMTP服务器端口 35 $smtpusermail = ""; //SMTP服务器的用户邮箱 36 $smtpuser = ""; //SMTP服务器的用户帐号 37 $smtppass = ""; //SMTP服务器的用户密码 38 $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); 39 //这里面的一个true是表示使用身份验证,否则不使用身份验证. 40 $emailtype = "HTML"; //信件类型,文本:text;网页:HTML 41 $smtpemailto = $email; 42 $smtpemailfrom = $smtpusermail; 43 $emailsubject = "www.jb51.net - 找回密码"; 44 $emailbody = "亲爱的".$email.":<br/>您在".$time."提交了找回密码请求。请点击下面的链接重置密码 45 (按钮24小时内有效)。<br/><a href='".$url."'target='_blank'>".$url."</a>"; 46 $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); 47 48 return $rs; 49 }
好了,这个时候你的邮箱将会收到一封来自helloweba的密码找回邮件,邮件内容中有一个URL链接,点击该链接到reset.php来验证邮箱。
代码如下
1 include_once("connect.php");//连接数据库 2 3 $token = stripslashes(trim($_GET['token'])); 4 $email = stripslashes(trim($_GET['email'])); 5 $sql = "select * from `t_user` where email='$email'"; 6 7 $query = mysql_query($sql); 8 $row = mysql_fetch_array($query); 9 if($row){ 10 $mt = md5($row['id'].$row['username'].$row['password']); 11 if($mt==$token){ 12 if(time()-$row['getpasstime']>24*60*60){ 13 $msg = '该链接已过期!'; 14 }else{ 15 //重置密码... 16 $msg = '请重新设置密码,显示重置密码表单,<br/>这里只是演示,略过。'; 17 } 18 }else{ 19 $msg = '无效的链接'; 20 } 21 }else{ 22 $msg = '错误的链接!'; 23 } 24 echo $msg;
reset.php首先接受参数email和token,然后根据email查询数据表t_user中是否存在该Email,如果存在则获取该用户的信息,并且和sendmail.php中的token组合方式一样构建token值,然后与url传过来的token进行对比,如果当前时间与发送邮件时的时间相差超过24小时的,则提示“该链接已过期!”,反之,则说明链接有效,并且调转到重置密码页面,最后就是用户自己设置新密码了。
小结:通过注册邮箱验证与本文邮件找回密码,我们知道发送邮件在网站开发中的应用以及它的重要性,当然,现在也流行短信验证应用,这个需要相关的短信接口对接就可以了。
最后,附上数据表t_user结构:
代码如下
1 CREATE TABLE `t_user` ( 2 `id` int(11) NOT NULL auto_increment, 3 `username` varchar(30) NOT NULL, 4 `password` varchar(32) NOT NULL, 5 `email` varchar(50) NOT NULL, 6 `getpasstime` int(10) NOT NULL, 7 PRIMARY KEY (`id`) 8 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
smtp.class.php类文件
代码如下
1 <?php 2 3 class Smtp{ 4 5 /* Public Variables */ 6 7 var $smtp_port; 8 9 var $time_out; 10 11 var $host_name; 12 13 var $log_file; 14 15 var $relay_host; 16 17 var $debug; 18 19 var $auth; 20 21 var $user; 22 23 var $pass; 24 25 /* Private Variables */ 26 var $sock; 27 28 /* Constractor */ 29 30 function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) { 31 $this->debug = false; 32 33 $this->smtp_port = $smtp_port; 34 35 $this->relay_host = $relay_host; 36 37 $this->time_out = 30; //is used in fsockopen() 38 39 $this->auth = $auth; //auth 40 41 $this->user = $user; 42 43 $this->pass = $pass; 44 45 $this->host_name = "localhost"; //is used in HELO command 46 $this->log_file = ""; 47 48 $this->sock = false; 49 } 50 51 /* Main Function */ 52 53 function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { 54 $mail_from = $this->get_address($this->strip_comment($from)); 55 56 $body = ereg_replace("(^|(rn))(.)", "1.3", $body); 57 58 $header .= "MIME-Version:1.0rn"; 59 60 if ($mailtype == "HTML") { 61 $header .= "Content-Type:text/htmlrn"; 62 } 63 64 $header .= "To: " . $to . "rn"; 65 66 if ($cc != "") { 67 $header .= "Cc: " . $cc . "rn"; 68 } 69 70 $header .= "From(www.jb51.net): $from<" . $from . ">rn"; 71 72 $header .= "Subject: " . $subject . "rn"; 73 74 $header .= $additional_headers; 75 76 $header .= "Date: " . date("r") . "rn"; 77 78 $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")rn"; 79 80 list ($msec, $sec) = explode(" ", microtime()); 81 82 $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">rn"; 83 84 $TO = explode(",", $this->strip_comment($to)); 85 86 if ($cc != "") { 87 $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); 88 } 89 90 if ($bcc != "") { 91 $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); 92 } 93 94 $sent = true; 95 96 foreach ($TO as $rcpt_to) { 97 $rcpt_to = $this->get_address($rcpt_to); 98 99 if (!$this->smtp_sockopen($rcpt_to)) { 100 $this->log_write("Error: Cannot send email to " . $rcpt_to . "n"); 101 102 $sent = false; 103 104 continue; 105 } 106 107 if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { 108 $this->log_write("E-mail has been sent to <" . $rcpt_to . ">n"); 109 } else { 110 $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">n"); 111 112 $sent = false; 113 } 114 115 fclose($this->sock); 116 117 $this->log_write("Disconnected from remote hostn"); 118 } 119 120 return $sent; 121 } 122 123 /* Private Functions */ 124 125 function smtp_send($helo, $from, $to, $header, $body = "") { 126 if (!$this->smtp_putcmd("HELO", $helo)) { 127 return $this->smtp_error("sending HELO command"); 128 } 129 // auth 130 if ($this->auth) { 131 if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { 132 return $this->smtp_error("sending HELO command"); 133 } 134 135 if (!$this->smtp_putcmd("", base64_encode($this->pass))) { 136 return $this->smtp_error("sending HELO command"); 137 } 138 } 139 140 if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) { 141 return $this->smtp_error("sending MAIL FROM command"); 142 } 143 144 if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) { 145 return $this->smtp_error("sending RCPT TO command"); 146 } 147 148 if (!$this->smtp_putcmd("DATA")) { 149 return $this->smtp_error("sending DATA command"); 150 } 151 152 if (!$this->smtp_message($header, $body)) { 153 return $this->smtp_error("sending message"); 154 } 155 156 if (!$this->smtp_eom()) { 157 return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); 158 } 159 160 if (!$this->smtp_putcmd("QUIT")) { 161 return $this->smtp_error("sending QUIT command"); 162 } 163 164 return true; 165 } 166 167 function smtp_sockopen($address) { 168 if ($this->relay_host == "") { 169 return $this->smtp_sockopen_mx($address); 170 } else { 171 return $this->smtp_sockopen_relay(); 172 } 173 } 174 175 function smtp_sockopen_relay() { 176 $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "n"); 177 178 $this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); 179 180 if (!($this->sock && $this->smtp_ok())) { 181 $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "n"); 182 183 $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); 184 185 return false; 186 } 187 188 $this->log_write("Connected to relay host " . $this->relay_host . "n"); 189 190 return true; 191 ; 192 } 193 194 function smtp_sockopen_mx($address) { 195 $domain = ereg_replace("^.+@([^@]+)$", "1", $address); 196 197 if (!@ getmxrr($domain, $MXHOSTS)) { 198 $this->log_write("Error: Cannot resolve MX "" . $domain . ""n"); 199 200 return false; 201 } 202 203 foreach ($MXHOSTS as $host) { 204 $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "n"); 205 206 $this->sock = @ fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); 207 208 if (!($this->sock && $this->smtp_ok())) { 209 $this->log_write("Warning: Cannot connect to mx host " . $host . "n"); 210 211 $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); 212 213 continue; 214 } 215 216 $this->log_write("Connected to mx host " . $host . "n"); 217 218 return true; 219 } 220 221 $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")n"); 222 223 return false; 224 } 225 226 function smtp_message($header, $body) { 227 fputs($this->sock, $header . "rn" . $body); 228 229 $this->smtp_debug("> " . str_replace("rn", "n" . "> ", $header . "n> " . $body . "n> ")); 230 231 return true; 232 } 233 234 function smtp_eom() { 235 fputs($this->sock, "rn.rn"); 236 237 $this->smtp_debug(". [EOM]n"); 238 239 return $this->smtp_ok(); 240 } 241 242 function smtp_ok() { 243 $response = str_replace("rn", "", fgets($this->sock, 512)); 244 245 $this->smtp_debug($response . "n"); 246 247 if (!ereg("^[23]", $response)) { 248 fputs($this->sock, "QUITrn"); 249 250 fgets($this->sock, 512); 251 252 $this->log_write("Error: Remote host returned "" . $response . ""n"); 253 254 return false; 255 } 256 257 return true; 258 } 259 260 function smtp_putcmd($cmd, $arg = "") { 261 if ($arg != "") { 262 if ($cmd == "") 263 $cmd = $arg; 264 265 else 266 $cmd = $cmd . " " . $arg; 267 } 268 269 fputs($this->sock, $cmd . "rn"); 270 271 $this->smtp_debug("> " . $cmd . "n"); 272 273 return $this->smtp_ok(); 274 } 275 276 function smtp_error($string) { 277 $this->log_write("Error: Error occurred while " . $string . ".n"); 278 279 return false; 280 } 281 282 function log_write($message) { 283 $this->smtp_debug($message); 284 285 if ($this->log_file == "") { 286 return true; 287 } 288 289 $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message; 290 291 if (!@ file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) { 292 $this->smtp_debug("Warning: Cannot open log file "" . $this->log_file . ""n"); 293 294 return false; 295 ; 296 } 297 298 flock($fp, LOCK_EX); 299 300 fputs($fp, $message); 301 302 fclose($fp); 303 304 return true; 305 } 306 307 function strip_comment($address) { 308 $comment = "([^()]*)"; 309 310 while (ereg($comment, $address)) { 311 $address = ereg_replace($comment, "", $address); 312 } 313 314 return $address; 315 } 316 317 function get_address($address) { 318 $address = ereg_replace("([ trn])+", "", $address); 319 320 $address = ereg_replace("^.*<(.+)>.*$", "1", $address); 321 322 return $address; 323 } 324 325 function smtp_debug($message) { 326 if ($this->debug) { 327 echo $message . " 328 ;"; 329 } 330 } 331 } 332 ?>
以上这篇PHP会员找回密码功能的简单实现就是小编分享给大家的全部内容了,希望能给大家一个参考
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/113277.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...