美工统筹SEO,为企业电子商务营销助力!
PHP会员找回暗码功效的简略完成
一佰互联网站建造(www.taishanly.com) 宣布日期 2020-04-26 09:03:51 阅读数: 101
设置思绪
1、用户注册时须要供给一个E-MAIL邮箱,目标便是用该邮箱找回暗码。
2、当用户健忘暗码或用户名时,点击登录页面的“找回暗码”超链接,翻开表单,并输出注册用的E-MAIL邮箱,提交。
3、体系经由进程该邮箱,从数据库中查找到该用户信息,并更新该用户的暗码为一个姑且暗码(比方:12345678)。
4、体系借助Jmail功效把该用户的信息发送到该用户的邮箱中(内容包含:用户名、姑且暗码、提醒用户实时点窜姑且暗码的提醒语)。
5、用户用姑且暗码便可登录。
HTML
咱们在找回暗码的页面上安排一个请求用户输出注册时所用的邮箱,而后提交前台js来处置交互。
代码以下
<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>
jQuery
当用户输出完邮箱并点击提交后,jQuery先考证邮箱格局是不是准确,若是准确则经由进程向背景sendmail.php发送Ajax请求,sendmail.php担任考证邮箱是不是存在和发送邮件,并会前往呼应的处置成果给前台页面,请看jQuery代码:
代码以下
$(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>"); } }); } }); })
以上利用的jQuery代码很便利简练的完成了前端交互操纵,若是您有必然的jQuery根本,那上面的代码一目明了,未几诠释。
固然别忘了在页面中加载jQuery库文件,有的同窗常常问我说从网高低载了demo怎样用不了,那80%是jquery或其余文件加载途径错了致使没加载须要的文件。
PHP
sendmail.php须要考证Email是不是存在体系用户表中,若是有,则读取用户信息,将用户id、用户名和暗码惊醒md5加密天生一个出格的字符串作为找回暗码的考证码,而后机关URL。同时咱们为了节制URL链接的时效性,将记实用户提交找回暗码举措的操纵时辰,最初挪用邮件发送类发送邮件到用户邮箱,发送邮件类smtp.class.php已打包好,请下载。
代码以下
include_once("connect.php");//毗连数据库 $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){//该邮箱还没有注册! echo "noreg"; exit; }else{ $row = mysql_fetch_array($query); $getpasstime = time(); $uid = $row["id"]; $token = md5($uid.$row["username"].$row["password"]);//组合考证码 $url = "/demo/resetpass/reset.php?email=".$email." &token=".$token;//机关URL $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){ include_once("smtp.class.php"); $smtpserver = ""; //SMTP办事器,如smtp.163.com $smtpserverport = 25; //SMTP办事器端口 $smtpusermail = ""; //SMTP办事器的用户邮箱 $smtpuser = ""; //SMTP办事器的用户帐号 $smtppass = ""; //SMTP办事器的用户暗码 $smtp = new Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这外面的一个true是表现利用身份考证,不然不利用身份考证. $emailtype = "HTML"; //函件范例,文本:text;网页:HTML $smtpemailto = $email; $smtpemailfrom = $smtpusermail; $emailsubject = "www.taishanly.com - 找回暗码"; $emailbody = "敬爱的".$email.":<br/>您在".$time."提交了找回暗码请求。请点击上面的链接重置暗码 (按钮24小时内有用)。<br/><a href="".$url.""target="_blank">".$url."</a>"; $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype); return $rs; }
好了,这个时辰你的邮箱将会收到一封来自helloweba的暗码找回邮件,邮件内容中有一个URL链接,点击该链接到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 = "有用的链接"; } }else{ $msg = "毛病的链接!"; } echo $msg;
reset.php起首接管参数email和token,而后按照email查问数据表t_user中是不是存在该Email,若是存在则获得该用户的信息,并且和sendmail.php中的token组合体例一样构建token值,而后与url传过去的token停止对照,若是以后时辰与发送邮件时的时辰相差跨越24小时的,则提醒“该链接已过时!”,反之,则申明链接有用,并且调转到重置暗码页面,最初便是用户本身设置新暗码了。
小结:经由进程注册邮箱考证与本文邮件找回暗码,咱们晓得发送邮件在网站开辟中的利用和它的主要性,固然,此刻也风行短信考证利用,这个须要相干的短信接口对接就能够了。
最初,附上数据表t_user布局:
代码以下
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;
smtp.class.php类文件
代码以下
<?php class Smtp{ /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; var $user; var $pass; /* Private Variables */ var $sock; /* Constractor */ function smtp($relay_host = "", $smtp_port = 25, $auth = false, $user, $pass) { $this->debug = false; $this->smtp_port = $smtp_port; $this->relay_host = $relay_host; $this->time_out = 30; //is used in fsockopen() $this->auth = $auth; //auth $this->user = $user; $this->pass = $pass; $this->host_name = "localhost"; //is used in HELO command $this->log_file = ""; $this->sock = false; } /* Main Function */ function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "") { $mail_from = $this->get_address($this->strip_comment($from)); $body = ereg_replace("(^|(rn))(.)", "1.3", $body); $header .= "MIME-Version:1.0rn"; if ($mailtype == "HTML") { $header .= "Content-Type:text/htmlrn"; } $header .= "To: " . $to . "rn"; if ($cc != "") { $header .= "Cc: " . $cc . "rn"; } $header .= "From(www.taishanly.com): $from<" . $from . ">rn"; $header .= "Subject: " . $subject . "rn"; $header .= $additional_headers; $header .= "Date: " . date("r") . "rn"; $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")rn"; list ($msec, $sec) = explode(" ", microtime()); $header .= "Message-ID: <" . date("YmdHis", $sec) . "." . ($msec * 1000000) . "." . $mail_from . ">rn"; $TO = explode(",", $this->strip_comment($to)); if ($cc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($cc))); } if ($bcc != "") { $TO = array_merge($TO, explode(",", $this->strip_comment($bcc))); } $sent = true; foreach ($TO as $rcpt_to) { $rcpt_to = $this->get_address($rcpt_to); if (!$this->smtp_sockopen($rcpt_to)) { $this->log_write("Error: Cannot send email to " . $rcpt_to . "n"); $sent = false; continue; } if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) { $this->log_write("E-mail has been sent to <" . $rcpt_to . ">n"); } else { $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">n"); $sent = false; } fclose($this->sock); $this->log_write("Disconnected from remote hostn"); } return $sent; } /* Private Functions */ function smtp_send($helo, $from, $to, $header, $body = "") { if (!$this->smtp_putcmd("HELO", $helo)) { return $this->smtp_error("sending HELO command"); } // auth if ($this->auth) { if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) { return $this->smtp_error("sending HELO command"); } if (!$this->smtp_putcmd("", base64_encode($this->pass))) { return $this->smtp_error("sending HELO command"); } } if (!$this->smtp_putcmd("MAIL", "FROM:<" . $from . ">")) { return $this->smtp_error("sending MAIL FROM command"); } if (!$this->smtp_putcmd("RCPT", "TO:<" . $to . ">")) { return $this->smtp_error("sending RCPT TO command"); } if (!$this->smtp_putcmd("DATA")) { return $this->smtp_error("sending DATA command"); } if (!$this->smtp_message($header, $body)) { return $this->smtp_error("sending message"); } if (!$this->smtp_eom()) { return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]"); } if (!$this->smtp_putcmd("QUIT")) { return $this->smtp_error("sending QUIT command"); } return true; } function smtp_sockopen($address) { if ($this->relay_host == "") { return $this->smtp_sockopen_mx($address); } else { return $this->smtp_sockopen_relay(); } } function smtp_sockopen_relay() { $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "n"); $this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "n"); $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); return false; } $this->log_write("Connected to relay host " . $this->relay_host . "n"); return true; ; } function smtp_sockopen_mx($address) { $domain = ereg_replace("^.+@([^@]+)$", "1", $address); if (!@ getmxrr($domain, $MXHOSTS)) { $this->log_write("Error: Cannot resolve MX "" . $domain . ""n"); return false; } foreach ($MXHOSTS as $host) { $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "n"); $this->sock = @ fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out); if (!($this->sock && $this->smtp_ok())) { $this->log_write("Warning: Cannot connect to mx host " . $host . "n"); $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); continue; } $this->log_write("Connected to mx host " . $host . "n"); return true; } $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", ", $MXHOSTS) . ")n"); return false; } function smtp_message($header, $body) { fputs($this->sock, $header . "rn" . $body); $this->smtp_debug("> " . str_replace("rn", "n" . "> ", $header . "n> " . $body . "n> ")); return true; } function smtp_eom() { fputs($this->sock, "rn.rn"); $this->smtp_debug(". [EOM]n"); return $this->smtp_ok(); } function smtp_ok() { $response = str_replace("rn", "", fgets($this->sock, 512)); $this->smtp_debug($response . "n"); if (!ereg("^[23]", $response)) { fputs($this->sock, "QUITrn"); fgets($this->sock, 512); $this->log_write("Error: Remote host returned "" . $response . ""n"); return false; } return true; } function smtp_putcmd($cmd, $arg = "") { if ($arg != "") { if ($cmd == "") $cmd = $arg; else $cmd = $cmd . " " . $arg; } fputs($this->sock, $cmd . "rn"); $this->smtp_debug("> " . $cmd . "n"); return $this->smtp_ok(); } function smtp_error($string) { $this->log_write("Error: Error occurred while " . $string . ".n"); return false; } function log_write($message) { $this->smtp_debug($message); if ($this->log_file == "") { return true; } $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message; if (!@ file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) { $this->smtp_debug("Warning: Cannot open log file "" . $this->log_file . ""n"); return false; ; } flock($fp, LOCK_EX); fputs($fp, $message); fclose($fp); return true; } function strip_comment($address) { $comment = "([^()]*)"; while (ereg($comment, $address)) { $address = ereg_replace($comment, "", $address); } return $address; } function get_address($address) { $address = ereg_replace("([ trn])+", "", $address); $address = ereg_replace("^.*<(.+)>.*$", "1", $address); return $address; } function smtp_debug($message) { if ($this->debug) { echo $message . " ;"; } } } ?>
最初面有个数据库毗连类,这里就不先容了大师能够百本站找相干的数据库毗连mysql类哦
以上这篇PHP会员找回暗码功效的简略完成便是小编分享给大师的全数内容了,但愿能给大师一个参考,也但愿大师多多撑持网页设想。