接《PHP+jQuery 注册模块的改进之一》继续修改:
①在注册成功后返回登录邮件页面( maillogin.php ),在页面中用户可以点击链接跳转到自己注册邮箱的登录页面,可以再次发送激活链接;
②给激活链接设置有效期,过了有效期点击激活连接,会跳转页面让用户选择再次发送邮件或者返回注册页;再次发送邮件后,进邮箱点击链接激活;
③点击重新发送激活连接,不跳转页面,在当前页无刷新弹出通知:已重新发送链接,几秒钟之后通知消失;
④点击更换收件邮箱,跳转至注册页
1.点击邮箱链接跳转至相应邮箱登陆界面
思路:
在maillogin.js中( 也可在php中设置 )设置常用邮件地址列表,当用户的注册邮箱和列表中的邮箱匹配时,则显示button"立即登录验证邮箱",点击则跳转至邮箱的登陆首页;如果用户注册的邮箱在列表中不存在时,则不显示登陆button。
如图,当邮箱列表中存在注册邮箱时:
当邮箱列表中不存在注册邮箱时:
当传递的参数不是有效的邮箱时提示参数错误:
maillogin.php:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>注册页面</title> 6 <link href="templets/css/common.css" rel="stylesheet" type="text/css"> 7 <style> 8 #container{ 9 10 background-color:#fff; 11 width:990px; 12 height:547px; 13 margin-top:50px; 14 margin-bottom:20px; 15 overflow:hidden; 16 } 17 #mailChk{ width:530px; margin:100px auto auto auto; position:relative;} 18 19 .mailfonts{ margin-left:20px;} 20 .mailpic{ float:left;} 21 .mailfonts{ float:left;} 22 .hfonts{ font-size:22px; } 23 .ftit{ 24 25 position:relative; 26 top:-70px; 27 left:-180px; 28 border-bottom:1px solid #eee; 29 width:870px; 30 padding-bottom:10px; 31 font-size: 20px; 32 font-weight: normal; 33 font-family: "Microsoft YaHei",\5fae\8f6f\96c5\9ed1,arial,\5b8b\4f53; 34 color: #323232; 35 } 36 .ftit2{ 37 38 height:1px; 39 top:50px; 40 left:-180px; 41 } 42 .sfonts{ line-height:48px; color:#666;} 43 .orange{ color:#ee8c18;} 44 #maillogin{ 45 46 display: block; 47 width: 390px; 48 height: 50px; 49 line-height: 50px; 50 border: 0; 51 overflow: hidden; 52 text-align: center; 53 background: #69b3f2; 54 font-family: "Microsoft YaHei",\5fae\8f6f\96c5\9ed1,arial,\5b8b\4f53; 55 font-size: 26px; 56 -webkit-border-radius: 2px; 57 -moz-border-radius: 2px; 58 border-radius: 2px; 59 margin:100px auto 0 85px; 60 cursor:pointer; 61 } 62 63 #maillogin:hover{ background: #7cbdf5;} 64 #maillogin,#maillogin:hover{ color:#fff;} 65 #maillogin a{ color:#fff;} 66 67 .notice{ 68 69 position:relative; 70 bottom:-70px; 71 left:-180px; 72 } 73 .notit{ font-size:14px; color:#949494; font-weight:bold; font-family:arial;} 74 .noul{ color:#949494; margin-left:-40px;} 75 </style> 76 <script src="templets/js/jquery-1.8.3.min.js"></script> 77 <script src="templets/js/maillogin.js"></script> 78 </head> 79 80 <body> 81 82 <!--顶部长条--> 83 <div id="header-nav"> 84 <div id="header-nav-fonts"> 85 <span class="top-tie-big"><a href="#">Dee‘s BLOG</a></span> 86 <span class="top-tie"> | </span> 87 <span class="top-tie-big">注册</span> 88 <span class="top-tie-small">已有帐号?马上<a href="#">登录</a></span> 89 <div class="cls"></div> 90 </div> 91 </div> 92 93 <!-- 内容区 --> 94 <div id="container"> 95 96 <div id="mailChk"> 97 <?php 98 99 if(isset($_GET[‘m‘]) && $_GET[‘m‘]!=""){ 100 101 $m = $_GET[‘m‘]; 102 }else{ 103 104 echo "<div class=\"hfonts ftit\">操作有误,<a href=\"templets/register.html\">请重新注册</div>"; 105 exit(); 106 } 107 ?> 108 <div class="hfonts ftit">邮箱验证</div> 109 <img class="mailpic" src="templets/images/mail.jpg"> 110 <div class="mailfonts"> 111 <div class="hfonts">验证邮件已发出,请48小时内登陆邮箱验证</div> 112 <div class="sfonts">登录邮箱 <a id="mailaddr" class="orange"><?php echo $m;?></a> ,并按邮件提示操作即可</div> 113 </div> 114 <button type="button" id="maillogin"><a href="">立即登录邮箱验证</a></button> 115 <div class="ftit ftit2"></div> 116 <div class="cls"></div> 117 <div class="notice"> 118 <h3 class="notit">还没有收到验证邮件呢?</h3> 119 <ul class="noul"> 120 <li>1.尝试到广告邮件、垃圾邮件目录里找找看</li> 121 <li>2.<a class="blue" href="#">再次发送验证邮件</a></li> 122 <li>3.如果重发注册验证邮件仍然没有收到,请更换<a class="blue" href="#">另一个邮件地址</a></li> 123 </ul> 124 </div> 125 </div> 126 127 </div> 128 </body> 129 </html>
maillogin.js:
1 $(function(){ 2 3 //邮箱地址 4 function mailaddr(mail){ 5 6 if(!mail.split("@")[1]){ 7 8 alert("参数错误"); 9 return false; 10 } 11 12 var mail = mail.split("@"); 13 14 mail = mail[1].toLowerCase(); 15 16 //与常用邮箱进行对比 17 if(mail == "qq.com" || mail == "vip.qq.com" || mail == "foxmail.com"){ 18 19 return ‘mail.qq.com‘; 20 }else if(mail == ‘163.com‘){ 21 22 return ‘mail.163.com‘; 23 }else if(mail == ‘vip.163.com‘){ 24 25 return ‘vip.163.com‘; 26 }else if(mail == ‘126.com‘){ 27 28 return‘mail.126.com‘; 29 }else if(mail == ‘gmail.com‘){ 30 31 return‘mail.google.com‘; 32 }else if(mail == ‘sohu.com‘){ 33 34 return‘mail.sohu.com‘; 35 }else if(mail == ‘tom.com‘){ 36 37 return‘mail.tom.com‘; 38 }else if(mail == ‘vip.sina.com‘){ 39 40 return‘vip.sina.com‘; 41 }else if(mail == ‘sina.com.cn‘ || mail == ‘sina.com‘){ 42 43 return‘mail.sina.com.cn‘; 44 }else if(mail == ‘tom.com‘){ 45 46 return‘mail.tom.com‘; 47 }else if(mail == ‘yahoo.com.cn‘ || mail == ‘yahoo.cn‘){ 48 49 return‘mail.cn.yahoo.com‘; 50 }else if(mail == ‘tom.com‘){ 51 52 return‘mail.tom.com‘; 53 }else if(mail == ‘yeah.net‘){ 54 55 return‘www.yeah.net‘; 56 }else if(mail == ‘21cn.com‘){ 57 58 return‘mail.21cn.com‘; 59 }else if(mail == ‘hotmail.com‘){ 60 61 return‘www.hotmail.com‘; 62 }else if(mail == ‘sogou.com‘){ 63 64 return‘mail.sogou.com‘; 65 }else if(mail == ‘188.com‘){ 66 67 return‘www.188.com‘; 68 }else if(mail == ‘139.com‘){ 69 70 return‘mail.10086.cn‘; 71 }else if(mail == ‘189.cn‘){ 72 73 return‘webmail15.189.cn/webmail‘; 74 }else if(mail == ‘wo.com.cn‘){ 75 76 return‘mail.wo.com.cn/smsmail‘; 77 }else if(mail == ‘139.com‘){ 78 79 return‘mail.10086.cn‘; 80 }else{ 81 82 return‘‘; 83 } 84 } 85 86 var uemailaddr = $("#mailaddr").text(); 87 88 if(mailaddr(uemailaddr) == ""){ 89 90 $("#maillogin").remove(); 91 //控制样式 92 $(".ftit2").css("top","110px"); 93 $(".notice").css("bottom","-120px"); 94 $("#mailaddr").click(function(){ 95 96 return false; 97 }); 98 }else{ 99 100 $("#mailaddr").attr("href","http://"+mailaddr(uemailaddr)); 101 $("#maillogin a").attr("href","http://"+mailaddr(uemailaddr)); 102 } 103 })
2.给激活链接设置失效时间
关于设置失效时间的目的,我在网上看到有这几种说法,第一是节省资源,过期的数据会被清除,这种说法我不认同,因为在过期之后,只是这一条激活连接不能使用,但是注册用户可以重新发送链接,所以用户数据并没有被清除;第二种说法是安全,即在较短时间内激活,可以避免用户邮箱被盗引发的安全问题,对于这一点我也不认同,因为即使用户邮箱被盗,还是可以重新发送激活链接,激活链接的地址仍然是在邮箱里面,所以和被盗不被盗没有关系;我认为是,在一个较长的时间内,如果网站的注册机制或者找回密码机制发生了变化,失效时间可以在一定程度上避免很早之前的激活链接仍按照以前的机制而带来激活错误或者找回密码错误,此时用户可以选择重新生成最新的激活链接来保证注册或者找回密码的正确性,对网站来说更稳定安全。
思路:
需要在数据库中增加两个字段,分别是表示用户的注册日期:regdate( int ),和激活链接是否有效的lockurl( tinyint )。
当用户注册成功时,把当前日期插入到表中的regdate字段;当用户超过有效期没有点击激活链接,再次点击激活链接时就把数据库中的lock字段修改成1,当lock为1时,该链接失效,直到用户重新生成注册链接时更换activekey后,把lock值设为0,同时更新注册时间,用户才可以点击新的链接发送邮件进行激活。
修改和新增的的文件有:
register_chk.php,注册成功时把当前的时间戳插入表中;
activation.php中在update user表的active字段之前,先判断是否超过有效期,如果超过,则active字段不变,同时lockurl字段设为1,此时给出提示:请重新激活并且跳转至reactivation.php页面;
reactivation.php接受参数,如果参数正确,则显示是重新收取激活链接还是返回注册页,如果参数错误则直接返回注册页;
当选择重新收取激活链接,则跳转至remail.php,如果参数正确,则更新注册时间和activekey,然后重新发送邮件,邮件中的地址依然指向activation.php。如果参数错误,则跳转回注册页。
其他修改之处:
1.把邮件代码写成邮件类
邮件注册类mail.class.php:
1 <?php 2 3 function postmail($auth,$port,$envelope,$password,$smtp,$postuemail,$subject,$mailbody){ 4 5 //$smtp smtp邮件服务器地址 6 //$postuemail 用户注册邮件 7 //$subject 邮件主题 8 //$mailbody 邮件内容 9 10 $config = array( 11 12 ‘auth‘=>$auth,//发件人 13 ‘port‘ =>$port,//端口 14 ‘username‘=>$envelope,//发件邮箱 15 ‘password‘=>$password //发件邮箱密码 16 ); 17 18 //实例化验证的对象,使用gmail smtp服务器 19 $transport = new Zend_Mail_Transport_Smtp($smtp,$config); 20 $mail = new Zend_Mail(‘utf-8‘); 21 22 $mail->addTo($postuemail,‘获取用户注册激活链接‘); 23 $mail->setFrom($envelope,$auth); 24 $mail->setSubject($subject); 25 $mail->setBodyHtml($mailbody); 26 $mail->send($transport); 27 }
2.用户名注册时区分大小写,修改checkname.php
//用户名区分大小写 $sql = "select uname from user where binary uname=‘".$uname."‘";
流程图:
需要注意的是,GET传递的参数也要进行转义处理。
Js倒计时跳转代码 showTime.js:
1 var textBox = document.getElementById("textBox"); 2 var second = document.getElementById("second"); 3 var t = 4; 4 function showTime(href){ 5 6 t-=1; 7 second.innerHTML = t; 8 if(t==0){ 9 10 textBox.innerHTML = "正在跳转..."; 11 self.location = href; 12 } 13 setTimeout("showTime(href)",1000); 14 }
注册并发送邮件代码 register_chk.php:
1 <?php 2 3 header("charst=utf-8"); 4 include_once ‘conn/conn.php‘; 5 include_once ‘Zend/Mail/Transport/Smtp.php‘; 6 include_once ‘Zend/Mail.php‘; 7 include_once ‘mail.class.php‘; 8 9 //激活key,生成的随机数 10 $key = md5(rand()); 11 12 //先写入数据库,再发邮件 13 //写入数据库 14 //判断是否开启magic_quotes_gpc 15 if(get_magic_quotes_gpc()){ 16 17 $postuname = $_POST[‘uname‘]; 18 $postupwd = $_POST[‘upwd‘]; 19 $postuemail = $_POST[‘uemail‘]; 20 }else{ 21 22 $postuname = addslashes($_POST[‘uname‘]); 23 $postupwd = addslashes($_POST[‘upwd‘]); 24 $postuemail = addslashes($_POST[‘uemail‘]); 25 } 26 27 function check_input($value){ 28 29 // 如果不是数字则加引号 30 if (!is_numeric($value)){ 31 32 $value = mysql_real_escape_string($value); 33 } 34 return $value; 35 } 36 37 $postuname = check_input($postuname); 38 $postupwd = check_input($postupwd); 39 $postuemail = check_input($postuemail); 40 41 $sql = "insert into user(uname,upwd,uemail,activekey,regdate)values(‘".trim($postuname)."‘,‘".md5(trim($postupwd))."‘,‘".trim($postuemail)."‘,‘".$key."‘,‘".time()."‘)"; 42 43 $num = $conne->uidRst($sql); 44 if($num == 1){ 45 46 //插入成功时发送邮件 47 //用户激活链接 48 $url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/activation.php‘; 49 //urlencode函数转换url中的中文编码 50 //带反斜杠 51 $url.= ‘?name=‘.urlencode(trim($postuname)).‘&k=‘.$key; 52 //file_put_contents("D:/mylog.log",$url." *********\r\n",FILE_APPEND); 53 //定义登录使用的邮箱 54 $envelope = ‘[email protected]‘; 55 $password = ‘换成邮箱的密码‘; 56 $port = 25; 57 $auth = ‘login‘; 58 $smtp = ‘smtp.126.com‘; 59 60 //激活邮件的主题和正文 61 $subject = ‘激活您的帐号‘; 62 $mailbody = ‘注册成功,<a href="‘.$url.‘" target="_blank">请点击此处激活帐号</a>‘; 63 64 postmail($auth,$port,$envelope,$password,$smtp,$postuemail,$subject,$mailbody); 65 66 echo "<script>self.location=\"maillogin.php?m=".$postuemail."\";</script>"; 67 68 }else{ 69 70 //提示激活失败并跳转 71 echo "<div id=\"textBox\">激活失败,请重新注册,<span id=\"second\"></span> 秒钟后跳转至重新激活页...</div>"; 72 echo "<script src=\"templets/js/showTime.js\"></script>"; 73 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 74 } 75 ?>
激活。activation.php:
1 <?php 2 session_start(); 3 header(‘Content-type:text/html;charset=utf-8‘); 4 include_once ‘conn/conn.php‘; 5 6 $nowTime = time(); 7 8 $table = "user"; 9 10 if(!empty($_GET[‘name‘]) && !is_null($_GET[‘name‘]) && !empty($_GET[‘k‘]) && !is_null($_GET[‘k‘])){ 11 12 if(get_magic_quotes_gpc()){ 13 14 $getname = stripslashes(urldecode($_GET[‘name‘])); 15 $k = stripslashes(urldecode($_GET[‘k‘])); 16 }else{ 17 18 $getname = urldecode($_GET[‘name‘]); 19 $k = urldecode($_GET[‘k‘]); 20 } 21 22 //urldecode反转url中的中文编码 23 $sql = "select * from ".$table." where uname=‘".$getname."‘ and activekey=‘".$k."‘"; 24 file_put_contents("D:/mylog.log",$sql." ---\r\n",FILE_APPEND); 25 $num = $conne->getRowsNum($sql); 26 27 if($num>0){ 28 29 $rs = $conne->getRowsRst($sql); 30 31 //注册时间 32 $rsRegdate = $rs[‘regdate‘]; 33 34 //有效期2分钟 35 if($nowTime > $rsRegdate+120){ 36 37 //超过有效期 38 $upnum = $conne->uidRst("update ".$table." set lockurl = 1 where uname = ‘".$getname."‘ and activekey = ‘".$k."‘"); 39 40 if($upnum>0){ 41 42 //提示激活失败并跳转 43 echo "<div id=\"textBox\">超过激活有效期,激活失败,<span id=\"second\"></span> 秒钟后跳转至重新激活页...</div>"; 44 echo "<script src=\"templets/js/showTime.js\"></script>"; 45 echo "<script>var href=‘reactivation.php?n=".urlencode($getname)."&k=".$k."‘;showTime(href);</script>"; 46 47 }else{ 48 49 //提示激活失败并跳转 50 echo "<div id=\"textBox\">激活失败,请重新注册,<span id=\"second\"></span> 秒钟后跳转至重新激活页...</div>"; 51 echo "<script src=\"templets/js/showTime.js\"></script>"; 52 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 53 } 54 }else{ 55 56 $upnum = $conne->uidRst("update ".$table." set active = 1 where uname = ‘".$getname."‘ and activekey = ‘".$k."‘"); 57 58 if($upnum>0){ 59 60 $_SESSION[‘name‘] = urldecode($getname); 61 echo "<script>alert(‘您已成功激活‘);window.location.href=‘main.php‘;</script>"; 62 }else{ 63 64 echo "<script>alert(‘您已经激活过了‘);window.location.href=‘main.php‘;</script>"; 65 } 66 } 67 }else{ 68 69 //提示激活失败并跳转 70 echo "激活链接已失效,请重新发送激活链接或重新注册"; 71 echo "<script>window.setTimeout(\"self.location=‘register.php‘\",3000);</script>"; 72 } 73 } 74 ?>
用户选择重新激活或返回注册页 reactivation.php:
1 <?php 2 header("charst=utf-8"); 3 4 if(isset($_GET[‘n‘]) && $_GET[‘n‘]!="" && isset($_GET[‘k‘]) && $_GET[‘k‘]!=""){ 5 6 //过滤处理 7 if(get_magic_quotes_gpc()){ 8 9 $n = stripslashes(urldecode($_GET[‘n‘])); 10 $k = stripslashes(urldecode($_GET[‘k‘])); 11 }else{ 12 13 $n = $_GET[‘n‘]; 14 $k = $_GET[‘k‘]; 15 } 16 17 echo "<a href=‘remail.php?n=".urlencode($n)."&k=".urlencode($k)."‘>重新收取激活邮件</a>"; 18 19 echo "<a href=‘register.php‘>返回注册页</a>"; 20 21 }else{ 22 23 echo "<div id=\"textBox\">参数错误,请重新注册,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 24 echo "<script src=\"templets/js/showTime.js\"></script>"; 25 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 26 } 27 ?>
重新发送邮件 remail.php:
1 <?php 2 header("charst=utf-8"); 3 include_once ‘conn/conn.php‘; 4 include_once ‘Zend/Mail/Transport/Smtp.php‘; 5 include_once ‘Zend/Mail.php‘; 6 include_once ‘mail.class.php‘; 7 8 if(isset($_GET[‘n‘]) && $_GET[‘n‘]!="" && isset($_GET[‘k‘]) && $_GET[‘k‘]!=""){ 9 10 //过滤处理 11 if(get_magic_quotes_gpc()){ 12 13 $n = stripslashes(urldecode($_GET[‘n‘])); 14 $k = stripslashes(urldecode($_GET[‘k‘])); 15 }else{ 16 17 $n = $_GET[‘n‘]; 18 $k = $_GET[‘k‘]; 19 } 20 21 $table = "user"; 22 //先查询记录 23 $sql = "select * from ".$table." where uname=‘".$n."‘ and activekey=‘".$k."‘"; 24 $rs = $conne->getRowsRst($sql); 25 $rsuemail = $rs[‘uemail‘]; 26 27 $num = $conne->getRowsNum($sql); 28 if($num == 1){ 29 30 $key = md5(rand()); 31 //更新注册时间 32 $nowTime = time(); 33 //update activekey和lockurl 34 $upnum = $conne->uidRst("update ".$table." set activekey = ‘".$key."‘ , lockurl=0,regdate = ‘".$nowTime."‘ where uname = ‘".$n."‘ and activekey = ‘".$k."‘"); 35 36 if($upnum == 1){ 37 38 //插入成功时发送邮件 39 //用户激活链接 40 $url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/activation.php‘; 41 //urlencode函数转换url中的中文编码 42 //带反斜杠 43 $url.= ‘?name=‘.urlencode((trim($n))).‘&k=‘.$key; 44 45 //定义登录使用的邮箱 46 $envelope = ‘[email protected]‘; 47 $password = ‘换成自己的邮箱密码‘; 48 $port = 25; 49 $auth = ‘login‘; 50 $smtp = ‘smtp.126.com‘; 51 52 //激活邮件的主题和正文 53 $subject = ‘激活您的帐号‘; 54 $mailbody = ‘注册成功,<a href="‘.$url.‘" target="_blank">请点击此处激活帐号</a>‘; 55 postmail($auth,$port,$envelope,$password,$smtp,$rsuemail,$subject,$mailbody); 56 57 echo "<script>self.location=\"maillogin.php?m=".$rsuemail."\";</script>"; 58 59 }else{ 60 61 //提示激活失败并跳转 62 echo "<div id=\"textBox\">激活失败,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 63 echo "<script src=\"templets/js/showTime.js\"></script>"; 64 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 65 } 66 67 68 }else{ 69 70 //提示激活失败并跳转 71 echo "<div id=\"textBox\">12激活失败,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 72 echo "<script src=\"templets/js/showTime.js\"></script>"; 73 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 74 } 75 }else{ 76 77 echo "<div id=\"textBox\">参数错误,请重新注册,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 78 echo "<script src=\"templets/js/showTime.js\"></script>"; 79 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 80 81 } 82 ?>
3.maillogin.php页面重新发送激活链接
思路: 注册页register_chk.php和重新发送邮件的remail.php页面在发送邮件以后跳至maillogin.php时,除了把email通过GET传递过去,再加上name和key,在maillogin.php页面把接受到的后两个参数接受到hidden的input里,通过jQuery的post方法请求remailajax.php重新发送邮件。发送成功以后通过jQuery设置定时器显示文字通知用户,4秒后自动消失。
如图:
代码:
register_chk.php:
1 <?php 2 3 header("charst=utf-8"); 4 include_once ‘conn/conn.php‘; 5 include_once ‘Zend/Mail/Transport/Smtp.php‘; 6 include_once ‘Zend/Mail.php‘; 7 include_once ‘mail.class.php‘; 8 9 //激活key,生成的随机数 10 $key = md5(rand()); 11 12 //先写入数据库,再发邮件 13 //写入数据库 14 //判断是否开启magic_quotes_gpc 15 if(get_magic_quotes_gpc()){ 16 17 $postuname = trim($_POST[‘uname‘]); 18 $postupwd = trim($_POST[‘upwd‘]); 19 $postuemail = trim($_POST[‘uemail‘]); 20 }else{ 21 22 $postuname = addslashes(trim($_POST[‘uname‘])); 23 $postupwd = addslashes(trim($_POST[‘upwd‘])); 24 $postuemail = addslashes(trim($_POST[‘uemail‘])); 25 } 26 27 function check_input($value){ 28 29 // 如果不是数字则加引号 30 if (!is_numeric($value)){ 31 32 $value = mysql_real_escape_string($value); 33 } 34 return $value; 35 } 36 37 $postuname = check_input($postuname); 38 $postupwd = check_input($postupwd); 39 $postuemail = check_input($postuemail); 40 41 $sql = "insert into user(uname,upwd,uemail,activekey,regdate)values(‘".$postuname."‘,‘".md5($postupwd)."‘,‘".$postuemail."‘,‘".$key."‘,‘".time()."‘)"; 42 43 $num = $conne->uidRst($sql); 44 if($num == 1){ 45 46 //插入成功时发送邮件 47 //用户激活链接 48 $url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/activation.php‘; 49 //urlencode函数转换url中的中文编码 50 //带反斜杠 51 $url.= ‘?name=‘.urlencode($postuname).‘&k=‘.$key; 52 53 //定义登录使用的邮箱 54 $envelope = ‘[email protected]‘; 55 $password = ‘邮箱密码‘; 56 $port = 25; 57 $auth = ‘login‘; 58 $smtp = ‘smtp.126.com‘; 59 60 //激活邮件的主题和正文 61 $subject = ‘激活您的帐号‘; 62 $mailbody = ‘注册成功,<a href="‘.$url.‘" target="_blank">请点击此处激活帐号</a>‘; 63 64 postmail($auth,$port,$envelope,$password,$smtp,$postuemail,$subject,$mailbody); 65 66 echo "<script>self.location=\"maillogin.php?m=".$postuemail."&n=".urlencode($postuname)."&k=".$key."\";</script>"; 67 68 }else{ 69 70 //提示激活失败并跳转 71 echo "<div id=\"textBox\">激活失败,请重新注册,<span id=\"second\"></span> 秒钟后跳转至重新激活页...</div>"; 72 echo "<script src=\"templets/js/showTime.js\"></script>"; 73 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 74 } 75 ?>
remail.php:
1 <?php 2 header("charst=utf-8"); 3 include_once ‘conn/conn.php‘; 4 include_once ‘Zend/Mail/Transport/Smtp.php‘; 5 include_once ‘Zend/Mail.php‘; 6 include_once ‘mail.class.php‘; 7 8 if(isset($_GET[‘n‘]) && $_GET[‘n‘]!="" && isset($_GET[‘k‘]) && $_GET[‘k‘]!=""){ 9 10 //过滤处理 11 if(get_magic_quotes_gpc()){ 12 13 $n = stripslashes(urldecode($_GET[‘n‘])); 14 $k = stripslashes(urldecode($_GET[‘k‘])); 15 }else{ 16 17 $n = $_GET[‘n‘]; 18 $k = $_GET[‘k‘]; 19 } 20 file_put_contents("D:/mylog.log",$n." xxx\r\n",FILE_APPEND); 21 $table = "user"; 22 //先查询记录 23 //addslashes($n) 因为使用了mysql_real_escape_string,所以存储在数据库中的是带转义的字符串 24 $sql = "select * from ".$table." where uname=‘".$n."‘ and activekey=‘".$k."‘"; 25 $rs = $conne->getRowsRst($sql); 26 $rsuemail = $rs[‘uemail‘]; 27 file_put_contents("D:/mylog.log",$sql." xxx\r\n",FILE_APPEND); 28 $num = $conne->getRowsNum($sql); 29 if($num == 1){ 30 31 $key = md5(rand()); 32 //更新注册时间 33 $nowTime = time(); 34 //update activekey和lockurl 35 $upnum = $conne->uidRst("update ".$table." set activekey = ‘".$key."‘ , lockurl=0,regdate = ‘".$nowTime."‘ where uname = ‘".$n."‘ and activekey = ‘".$k."‘"); 36 37 if($upnum == 1){ 38 39 //插入成功时发送邮件 40 //用户激活链接 41 $url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/activation.php‘; 42 //urlencode函数转换url中的中文编码 43 //带反斜杠 44 $url.= ‘?name=‘.urlencode((trim($n))).‘&k=‘.$key; 45 46 //定义登录使用的邮箱 47 $envelope = ‘[email protected]‘; 48 $password = ‘邮箱密码‘; 49 $port = 25; 50 $auth = ‘login‘; 51 $smtp = ‘smtp.126.com‘; 52 53 //激活邮件的主题和正文 54 $subject = ‘激活您的帐号‘; 55 $mailbody = ‘注册成功,<a href="‘.$url.‘" target="_blank">请点击此处激活帐号</a>‘; 56 postmail($auth,$port,$envelope,$password,$smtp,$rsuemail,$subject,$mailbody); 57 58 echo "<script>self.location=\"maillogin.php?m=".$rsuemail."&n=".urlencode($n)."&k=".$key."\";</script>"; 59 }else{ 60 61 //提示激活失败并跳转 62 echo "<div id=\"textBox\">22激活失败,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 63 echo "<script src=\"templets/js/showTime.js\"></script>"; 64 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 65 } 66 67 68 }else{ 69 70 //提示激活失败并跳转 71 echo "<div id=\"textBox\">12激活失败,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 72 echo "<script src=\"templets/js/showTime.js\"></script>"; 73 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 74 } 75 }else{ 76 77 echo "<div id=\"textBox\">参数错误,请重新注册,<span id=\"second\"></span> 秒钟后跳转至注册页...</div>"; 78 echo "<script src=\"templets/js/showTime.js\"></script>"; 79 echo "<script>var href=‘register.php‘;showTime(href);</script>"; 80 81 } 82 ?>
maillogin.php:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>注册页面</title> 6 <link href="templets/css/common.css" rel="stylesheet" type="text/css"> 7 <style> 8 #container{ 9 10 background-color:#fff; 11 width:990px; 12 height:547px; 13 margin-top:50px; 14 margin-bottom:20px; 15 overflow:hidden; 16 } 17 #mailChk{ width:530px; margin:100px auto auto auto; position:relative;} 18 19 .mailfonts{ margin-left:20px;} 20 .mailpic{ float:left;} 21 .mailfonts{ float:left;} 22 .hfonts{ font-size:22px; } 23 .ftit{ 24 25 position:relative; 26 top:-70px; 27 left:-180px; 28 border-bottom:1px solid #eee; 29 width:870px; 30 padding-bottom:10px; 31 font-size: 20px; 32 font-weight: normal; 33 font-family: "Microsoft YaHei",\5fae\8f6f\96c5\9ed1,arial,\5b8b\4f53; 34 color: #323232; 35 } 36 .ftit2{ 37 38 height:1px; 39 top:50px; 40 left:-180px; 41 } 42 .sfonts{ line-height:48px; color:#666;} 43 .orange{ color:#ee8c18;} 44 #maillogin{ 45 46 display: block; 47 width: 390px; 48 height: 50px; 49 line-height: 50px; 50 border: 0; 51 overflow: hidden; 52 text-align: center; 53 background: #69b3f2; 54 font-family: "Microsoft YaHei",\5fae\8f6f\96c5\9ed1,arial,\5b8b\4f53; 55 font-size: 26px; 56 -webkit-border-radius: 2px; 57 -moz-border-radius: 2px; 58 border-radius: 2px; 59 margin:100px auto 0 85px; 60 cursor:pointer; 61 } 62 63 #maillogin:hover{ background: #7cbdf5;} 64 #maillogin,#maillogin:hover{ color:#fff;} 65 #maillogin a{ color:#fff;} 66 67 .notice{ 68 69 position:relative; 70 bottom:-70px; 71 left:-180px; 72 } 73 #notice{ 74 75 width:300px; 76 height:200px; 77 position:fixed; 78 top:50%; 79 left:50%; 80 margin:-100px 0 0 -150px; 81 background:#eee; 82 text-align:center; 83 line-height:180px; 84 display:none; 85 } 86 .notit{ font-size:14px; color:#949494; font-weight:bold; font-family:arial;} 87 .noul{ color:#949494; margin-left:-40px;} 88 </style> 89 <script src="templets/js/jquery-1.8.3.min.js"></script> 90 <script src="templets/js/maillogin.js"></script> 91 </head> 92 93 <body> 94 95 <!--顶部长条--> 96 <div id="header-nav"> 97 <div id="header-nav-fonts"> 98 <span class="top-tie-big"><a href="#">Dee‘s BLOG</a></span> 99 <span class="top-tie"> | </span> 100 <span class="top-tie-big">注册</span> 101 <span class="top-tie-small">已有帐号?马上<a href="#">登录</a></span> 102 <div class="cls"></div> 103 </div> 104 </div> 105 106 <!-- 内容区 --> 107 <div id="container"> 108 109 <div id="mailChk"> 110 <?php 111 112 if(isset($_GET[‘m‘]) && $_GET[‘m‘]!=""){ 113 114 $m = $_GET[‘m‘]; 115 }else{ 116 117 echo "<div class=\"hfonts ftit\">操作有误,<a href=\"templets/register.html\">请重新注册</div>"; 118 exit(); 119 } 120 121 //接收用户名和key 122 if(!empty($_GET[‘n‘]) && !is_null($_GET[‘n‘]) && !empty($_GET[‘k‘]) && !is_null($_GET[‘k‘])){ 123 124 if(get_magic_quotes_gpc()){ 125 126 $n = stripslashes(urldecode($_GET[‘n‘])); 127 $k = stripslashes(urldecode($_GET[‘k‘])); 128 }else{ 129 130 $n = urldecode($_GET[‘n‘]); 131 $k = urldecode($_GET[‘k‘]); 132 } 133 } 134 135 require_once ‘mail.class.php‘; 136 ?> 137 <div class="hfonts ftit">邮箱验证</div> 138 <img class="mailpic" src="templets/images/mail.jpg"> 139 <div class="mailfonts"> 140 <div class="hfonts">验证邮件已发出,请48小时内登陆邮箱验证</div> 141 <div class="sfonts">登录邮箱 <a id="mailaddr" class="orange"><?php echo $m;?></a> ,并按邮件提示操作即可</div> 142 <input id="n" type="hidden" value="<?php echo $n;?>"/> 143 <input id="k" type="hidden" value="<?php echo $k;?>"/> 144 </div> 145 <button type="button" id="maillogin"><a href="">立即登录邮箱验证</a></button> 146 <div class="ftit ftit2"></div> 147 <div class="cls"></div> 148 <div class="notice"> 149 <h3 class="notit">还没有收到验证邮件呢?</h3> 150 <ul class="noul"> 151 <li>1.尝试到广告邮件、垃圾邮件目录里找找看</li> 152 <li>2.<a id="re" class="blue" href="#">再次发送验证邮件</a></li> 153 <li>3.如果重发注册验证邮件仍然没有收到,请更换<a class="blue" href="register.php">另一个邮件地址</a></li> 154 </ul> 155 </div> 156 </div> 157 158 </div> 159 </body> 160 <script> 161 162 $(function(){ 163 164 $("#re").click(function(){ 165 166 $.post("remailajax.php",{ 167 168 //要传递的数据 169 n : $("#n").val(), 170 k : $("#k").val(), 171 m : $("#mailaddr").text() 172 },function(data,textStatus){ 173 174 if(data == 0){ 175 176 success(); 177 }else{ 178 179 alert("重新发送邮件失败"); 180 } 181 }); 182 183 return false; 184 }); 185 186 187 $notice = $("<div id=\"notice\">邮件已经发送,请到邮箱中查看...</div>"); 188 function success(){ 189 190 $notice.insertAfter($("#mailChk")); 191 192 $notice.show(300); 193 } 194 195 function hide(){ 196 197 if($notice){ 198 199 $notice.hide(300); 200 } 201 } 202 203 //显示4秒 204 setInterval(hide,4000); 205 }); 206 </script> 207 </html>
emailajax.php:
<?php header("charset=utf-8"); include_once ‘Zend/Mail/Transport/Smtp.php‘; include_once ‘Zend/Mail.php‘; require_once ‘mail.class.php‘; if(isset($_POST[‘n‘]) && $_POST[‘n‘]!="" && isset($_POST[‘k‘]) && $_POST[‘k‘]!="" && isset($_POST[‘m‘]) && $_POST[‘m‘]!=""){ if(get_magic_quotes_gpc()){ $n = stripslashes(urldecode($_POST[‘n‘])); $k = stripslashes(urldecode($_POST[‘k‘])); $m = stripslashes(urldecode($_POST[‘m‘])); }else{ $n = $_POST[‘n‘]; $k = $_POST[‘k‘]; $m = $_POST[‘m‘]; } $url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].‘/activation.php‘; //urlencode函数转换url中的中文编码 //带反斜杠 $url.= ‘?name=‘.urlencode($n).‘&k=‘.$k; //定义登录使用的邮箱 $envelope = ‘[email protected]‘; $password = ‘邮箱密码‘; $port = 25; $auth = ‘login‘; $smtp = ‘smtp.126.com‘; //激活邮件的主题和正文 $subject = ‘激活您的帐号‘; $mailbody = ‘注册成功,<a href="‘.$url.‘" target="_blank">请点击此处激活帐号</a>‘; postmail($auth,$port,$envelope,$password,$smtp,$m,$subject,$mailbody); echo 0; }
4.更换邮箱即重新注册。
maillogin.php代码片段:
<li>3.如果重发注册验证邮件仍然没有收到,请更换<a class="blue" href="register.php">另一个邮件地址</a></li>
5.此时目录结构和数据库
ROOT:
├─conn
│ ├─conn.php
│
├─templets
│ ├─css
│ │ ├─common.css
│ │ ├─register.css
│ │
│ ├─images
│ │
│ └─js
│ ├─jquery-1.8.3.min.js
│ ├─register.js
│ ├─emailup.js
│ ├─showTime.js
│ ├─maillogin.js
│
├─chkname.php
├─chkemail.php
├─valcode.php
├─register_chk.php
├─activation.php
├─mail.class.php -- 邮件类
├─maillogin.php --发送完邮件的返回页,可以登录邮箱和再次发送邮件
├─reactivation.php --用户选择是重新发送激活邮件还是返回注册页
├─remail.php --超时重新发送邮件
├─remailajax.php --点击再次发送邮件
├─remail.php --重新发送邮件
├─showval.php --生产验证码
├─arial.ttf
│
└─Zend
数据库:
1 CREATE TABLE `user` ( 2 `id` int(11) NOT NULL auto_increment, 3 `uname` varchar(14) character set utf8 collate utf8_bin NOT NULL default ‘‘, 4 `upwd` varchar(32) NOT NULL default ‘‘, 5 `uemail` varchar(50) NOT NULL default ‘‘, 6 `active` tinyint(4) default ‘0‘, 7 `count` tinyint(4) default ‘0‘, 8 `activekey` varchar(32) NOT NULL default ‘‘ COMMENT ‘注册时生成的key,用于激活验证‘, 9 `regdate` int(11) default NULL COMMENT ‘注册时间‘, 10 `lockurl` tinyint(4) default ‘0‘ COMMENT ‘超过激活时间m没有激活,就把该值置为1‘, 11 PRIMARY KEY (`id`), 12 UNIQUE KEY `uemail` (`uemail`) 13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
作者:小dee
出处:http://www.cnblogs.com/dee0912/
说明:作者写博目的是记录开发过程,积累经验,便于以后工作参考。
如需转载,请在文章页面保留此说明并且给出原文链接。谢谢!