php专区

 首页 > php专区 > PHP应用 > 常用功能 > PHP会员找回密码功能实现实例介绍 - php高级应用

PHP会员找回密码功能实现实例介绍 - php高级应用

分享到:
【字体:
导读:
          如果你做的网站有会员系统那必须就自动找回密码功能了,也就是忘记密码功能,如果用户忘记了密码可以通过邮箱或手机号直接找回密码,下面我来...

PHP会员找回密码功能实现实例介绍

如果你做的网站有会员系统那必须就自动找回密码功能了,也就是忘记密码功能,如果用户忘记了密码可以通过邮箱或手机号直接找回密码,下面我来介绍邮箱找回密码方法。

设置思路

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("

    "+msg+"

    "
    );  
  15.                 }  
  16.             });  
  17.         }  
  18.     });  
  19. }) 

以上使用的jQuery代码很方便简洁的完成了前端交互操作,如果您有一定的jQuery基础,那上面的代码一目了然,不多解释。

当然别忘了在页面中加载jQuery库文件,有的同学经常问我说从www.phpfensi.com下载了demo怎么用不了,那80%是jquery或者其他文件加载路径错了导致没加载必要的文件。

PHP:sendmail.php需要验证Email是否存在系统用户表中,如果有,则读取用户信息,将用户id、用户名和密码惊醒md5加密生成一个特别的字符串作为找回密码的验证码,然后构造URL,同时我们为了控制URL链接的时效性,将记录用户提交找回密码动作的操作时间,最后调用邮件发送类发送邮件到用户邮箱,发送邮件类smtp.class.php已经打包好,请下载,代码如下:

  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 = '系统已向您的邮箱发送了一封邮件
    请登录到您的邮箱及时重置您的密码!'
    ;  
  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.111cn.net - 找回密码";  
  44.     $emailbody = "亲爱的".$email.":
    您在"
    .$time."提交了找回密码请求。请点击下面的链接重置密码  
  45. (按钮24小时内有效)。
    '".$url."'
    target='_blank'>".$url."";  
  46.     $rs = $smtp->sendmail($smtpemailto$smtpemailfrom$emailsubject$emailbody$emailtype);  
  47.  
  48.     return $rs;  

好了,这个时候你的邮箱将会收到一封来自helloweba的密码找回邮件,邮件内容中有一个URL链接,点击该链接到www.phpfensi.com的reset.php来验证邮箱,代码如下:

  1. include_once("connect.php");//连接数据库  
  2. $token = stripslashes(trim($_GET['token']));  
  3. $email = stripslashes(trim($_GET['email']));  
  4. $sql = "select * from `t_user` where email='$email'";  
  5. $query = mysql_query($sql);  
  6. $row = mysql_fetch_array($query);  
  7. if($row){  
  8.     $mt = md5($row['id'].$row['username'].$row['password']);  
  9.     if($mt==$token){  
  10.         if(time()-$row['getpasstime']>24*60*60){  
  11.             $msg = '该链接已过期!';  
  12.         }else{  
  13.             //重置密码...  
  14.             $msg = '请重新设置密码,显示重置密码表单,
    这里只是演示,略过。'
    ;  
  15.         }  
  16.     }else{  
  17.         $msg =  '无效的链接';  
  18.     }  
  19. }else{  
  20.     $msg =  '错误的链接!';      
  21. }  
  22. 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.   `passwordvarchar(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. class Smtp{ 
  2.     /* Public Variables */ 
  3.  var $smtp_port
  4.  var $time_out
  5.  var $host_name
  6.  var $log_file
  7.  var $relay_host
  8.  var $debug
  9.  var $auth
  10.  var $user
  11.  var $pass
  12.  /* Private Variables */ 
  13.  var $sock
  14.  /* Constractor */ 
  15.  function smtp($relay_host = ""$smtp_port = 25, $auth = false, $user$pass) { 
  16.   $this->debug = false; 
  17.   $this->smtp_port = $smtp_port
  18.   $this->relay_host = $relay_host
  19.   $this->time_out = 30; //is used in fsockopen() 
  20.   $this->auth = $auth//auth 
  21.   $this->user = $user
  22.   $this->pass = $pass
  23.   $this->host_name = "localhost"//is used in HELO command 
  24.   $this->log_file = ""
  25.   $this->sock = false; 
  26.  } 
  27.  /* Main Function */ 
  28.  function sendmail($to$from$subject = ""$body = ""$mailtype$cc = ""$bcc = ""$additional_headers = "") { 
  29.   $mail_from = $this->get_address($this->strip_comment($from)); 
  30.   $body = ereg_replace("(^|(rn))(.)""1.3"$body); 
  31.   $header .= "MIME-Version:1.0rn"
  32.   if ($mailtype == "HTML") { 
  33.    $header .= "Content-Type:text/htmlrn"
  34.   } 
  35.   $header .= "To: " . $to . "rn"
  36.   if ($cc != "") { 
  37.    $header .= "Cc: " . $cc . "rn"
  38.   } 
  39.   $header .= "From: $from<" . $from . ">rn"
  40.   $header .= "Subject: " . $subject . "rn"
  41.   $header .= $additional_headers
  42.   $header .= "Date: " . date("r") . "rn"
  43.   $header .= "X-Mailer:By Redhat (PHP/" . phpversion() . ")rn"
  44.   list ($msec$sec) = explode(" ", microtime()); 
  45.   $header .= "Message-ID: <" . date("YmdHis"$sec) . "." . ($msec * 1000000) . "." . $mail_from . ">rn"
  46.   $TO = explode(","$this->strip_comment($to)); 
  47.   if ($cc != "") { 
  48.    $TO = array_merge($TOexplode(","$this->strip_comment($cc))); 
  49.   } 
  50.   if ($bcc != "") { 
  51.    $TO = array_merge($TOexplode(","$this->strip_comment($bcc))); 
  52.   } 
  53.   $sent = true; 
  54.   foreach ($TO as $rcpt_to) { 
  55.    $rcpt_to = $this->get_address($rcpt_to); 
  56.    if (!$this->smtp_sockopen($rcpt_to)) { 
  57.     $this->log_write("Error: Cannot send email to " . $rcpt_to . "n"); 
  58.     $sent = false; 
  59.     continue
  60.    } 
  61.    if ($this->smtp_send($this->host_name, $mail_from$rcpt_to$header$body)) { 
  62.     $this->log_write("E-mail has been sent to <" . $rcpt_to . ">n"); 
  63.    } else { 
  64.     $this->log_write("Error: Cannot send email to <" . $rcpt_to . ">n"); 
  65.     $sent = false; 
  66.    } 
  67.    fclose($this->sock); 
  68.    $this->log_write("Disconnected from remote hostn"); 
  69.   } 
  70.   return $sent
  71.  } 
  72.  /* Private Functions */ 
  73.  function smtp_send($helo$from$to$header$body = "") { 
  74.   if (!$this->smtp_putcmd("HELO"$helo)) { 
  75.    return $this->smtp_error("sending HELO command"); 
  76.   } 
  77.   // auth 
  78.   if ($this->auth) { 
  79.    if (!$this->smtp_putcmd("AUTH LOGIN"base64_encode($this->user))) { 
  80.     return $this->smtp_error("sending HELO command"); 
  81.    } 
  82.    if (!$this->smtp_putcmd(""base64_encode($this->pass))) { 
  83.     return $this->smtp_error("sending HELO command"); 
  84.    } 
  85.   } 
  86.   if (!$this->smtp_putcmd("MAIL""FROM:<" . $from . ">")) { 
  87.    return $this->smtp_error("sending MAIL FROM command"); 
  88.   } 
  89.   if (!$this->smtp_putcmd("RCPT""TO:<" . $to . ">")) { 
  90.    return $this->smtp_error("sending RCPT TO command"); 
  91.   } 
  92.   if (!$this->smtp_putcmd("DATA")) { 
  93.    return $this->smtp_error("sending DATA command"); 
  94.   } 
  95.   if (!$this->smtp_message($header$body)) { 
  96.    return $this->smtp_error("sending message"); 
  97.   } 
  98.   if (!$this->smtp_eom()) { 
  99.    return $this->smtp_error("sending . [EOM]"); 
  100.   } 
  101.   if (!$this->smtp_putcmd("QUIT")) { 
  102.    return $this->smtp_error("sending QUIT command"); 
  103.   } 
  104.   return true; 
  105.  } 
  106.  function smtp_sockopen($address) { 
  107.   if ($this->relay_host == "") { 
  108.    return $this->smtp_sockopen_mx($address); 
  109.   } else { 
  110.    return $this->smtp_sockopen_relay(); 
  111.   } 
  112.  } 
  113.  function smtp_sockopen_relay() { 
  114.   $this->log_write("Trying to " . $this->relay_host . ":" . $this->smtp_port . "n"); 
  115.   $this->sock = @ fsockopen($this->relay_host, $this->smtp_port, $errno$errstr$this->time_out); 
  116.   if (!($this->sock && $this->smtp_ok())) { 
  117.    $this->log_write("Error: Cannot connenct to relay host " . $this->relay_host . "n"); 
  118.    $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); 
  119.    return false; 
  120.   } 
  121.   $this->log_write("Connected to relay host " . $this->relay_host . "n"); 
  122.   return true; 
  123.   ; 
  124.  } 
  125.  function smtp_sockopen_mx($address) { 
  126.   $domain = ereg_replace("^.+@([^@]+)$""1"$address); 
  127.   if (!@ getmxrr($domain$MXHOSTS)) { 
  128.    $this->log_write("Error: Cannot resolve MX "" . $domain . ""n"); 
  129.    return false; 
  130.   } 
  131.   foreach ($MXHOSTS as $host) { 
  132.    $this->log_write("Trying to " . $host . ":" . $this->smtp_port . "n"); 
  133.    $this->sock = @ fsockopen($host$this->smtp_port, $errno$errstr$this->time_out); 
  134.    if (!($this->sock && $this->smtp_ok())) { 
  135.     $this->log_write("Warning: Cannot connect to mx host " . $host . "n"); 
  136.     $this->log_write("Error: " . $errstr . " (" . $errno . ")n"); 
  137.     continue
  138.    } 
  139.    $this->log_write("Connected to mx host " . $host . "n"); 
  140.    return true; 
  141.   } 
  142.   $this->log_write("Error: Cannot connect to any mx hosts (" . implode(", "$MXHOSTS) . ")n"); 
  143.   return false; 
  144.  } 
  145.  function smtp_message($header$body) { 
  146.   fputs($this->sock, $header . "rn" . $body); 
  147.   $this->smtp_debug("> " . str_replace("rn""n" . "> "$header . "n> " . $body . "n> ")); 
  148.   return true; 
  149.  } 
  150.  function smtp_eom() { 
  151.   fputs($this->sock, "rn.rn"); 
  152.   $this->smtp_debug(". [EOM]n"); 
  153.   return $this->smtp_ok(); 
  154.  } 
  155.  function smtp_ok() { 
  156.   $response = str_replace("rn"""fgets($this->sock, 512)); 
  157.   $this->smtp_debug($response . "n"); 
  158.   if (!ereg("^[23]"$response)) { 
  159.    fputs($this->sock, "QUITrn"); 
  160.    fgets($this->sock, 512); 
  161.    $this->log_write("Error: Remote host returned "" . $response . ""n"); 
  162.    return false; 
  163.   } 
  164.   return true; 
  165.  } 
  166.  function smtp_putcmd($cmd$arg = "") { 
  167.   if ($arg != "") { 
  168.    if ($cmd == ""
  169.     $cmd = $arg
  170.    else 
  171.     $cmd = $cmd . " " . $arg
  172.   } 
  173.   fputs($this->sock, $cmd . "rn"); 
  174.   $this->smtp_debug("> " . $cmd . "n"); 
  175.   return $this->smtp_ok(); 
  176.  } 
  177.  function smtp_error($string) { 
  178.   $this->log_write("Error: Error occurred while " . $string . ".n"); 
  179.   return false; 
  180.  } 
  181.  function log_write($message) { 
  182.   $this->smtp_debug($message); 
  183.   if ($this->log_file == "") { 
  184.    return true; 
  185.   } 
  186.   $message = date("M d H:i:s ") . get_current_user() . "[" . getmypid() . "]: " . $message
  187.   if (!@ file_exists($this->log_file) || !($fp = @ fopen($this->log_file, "a"))) { 
  188.    $this->smtp_debug("Warning: Cannot open log file "" . $this->log_file . ""n"); 
  189.    return false; 
  190.    ; 
  191.   } 
  192.   flock($fp, LOCK_EX); 
  193.   fputs($fp$message); 
  194.   fclose($fp); 
  195.   return true; 
  196.  } 
  197.  function strip_comment($address) { 
  198.   $comment = "([^()]*)"
  199.   while (ereg($comment$address)) { 
  200.    $address = ereg_replace($comment""$address); 
  201.   } 
  202.   return $address
  203.  } 
  204.  function get_address($address) { 
  205.   $address = ereg_replace("([ trn])+"""$address); 
  206.   $address = ereg_replace("^.*<(.+)>.*$""1"$address); 
  207.   return $address
  208.  } 
  209.  function smtp_debug($message) { 
  210.   if ($this->debug) { 
  211.    echo $message . " 
  212.    ;"; 
  213.   } 
  214.  } 
  215. ?> 

最后面有个数据库连接类,这里就不介绍了大大家可以百本站找相关的数据库连接mysql类哦。

分享到:
php 通过curl post发送json数据实例 - ph...
php 通过curl post发送json数据实例 利用php curl发送json数据与curl post其它数据是一样的,下面我来给大家总结几个关于curl post发送json数据实例,希望能加深各位对curl post json数据的理解吧。 例1代码如下: $data = array("name" => "Hagrid", "age" => "36");      ...
Drupal 通过cURL Post方式发送一个文件 -...
Drupal 通过cURL Post方式发送一个文件 众所周知PHP的cURL扩展可以用来模拟表单提交,在Drupal中有drupal_http_request函数来执行一个HTTP请求,它可以通过POST方式来发送一个文件,但是使用起来没有cURL那么方便,这里我们主要讲解如何在Drupal中Post一个文件到远程的服务器地址。 网页Form表...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……