PHP用户注册邮箱验证激活帐号
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
我们在很多网站注册会员时,注册完成后,系统会自动向用户的邮箱发送一封邮件,这封邮件的内容就是一个URL链接,用户需要点击打开这个链接才能激活之前在该网站注册的帐号。激活成功后才能正常使用会员功能。
本文将结合实例,讲解如何使用PHP+Mysql完成注册帐号、发送激活邮件、验证激活帐号、处理URL链接过期的功能。
业务流程
1、用户提交注册信息。
2、写入数据库,此时帐号状态未激活。
3、将用户名密码或其他标识字符加密构造成激活识别码(你也可以叫激活码)。
4、将构造好的激活识别码组成URL发送到用户提交的邮箱。
5、用户登录邮箱并点击URL,进行激活。
6、验证激活识别码,如果正确则激活帐号。
准备数据表
用户信息表中字段Email很重要,它可以用来验证用户、找回密码、甚至对网站方来说可以用来收集用户信息进行Email营销,以下是用户信息表t_user的表结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CREATE TABLE IF NOT EXISTS `t_user` ( `id` int(<span class="php__number" style="color: red;">11</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> AUTO_INCREMENT, `username` varchar(<span class="php__number" style="color: red;">30</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'用户名'</span>, `password` varchar(<span class="php__number" style="color: red;">32</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'密码'</span>, `email` varchar(<span class="php__number" style="color: red;">30</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'邮箱'</span>, `token` varchar(<span class="php__number" style="color: red;">50</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'帐号激活码'</span>, `token_exptime` int(<span class="php__number" style="color: red;">10</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'激活码有效期'</span>, `status` tinyint(<span class="php__number" style="color: red;">1</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> DEFAULT <span class="php__string1" style="color: purple;">'0'</span> COMMENT <span class="php__string1" style="color: purple;">'状态,0-未激活,1-已激活'</span>, `regtime` int(<span class="php__number" style="color: red;">10</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span> COMMENT <span class="php__string1" style="color: purple;">'注册时间'</span>, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
HTML
在页面中放置一个注册表单,用户可以输入注册信息,包括用户名、密码和邮箱。
1 2 3 4 5 6 7 8 |
<span class="html__tag_start" style="font-weight: bold; color: navy;"><form</span> <span class="html__attr_name" style="color: green;">id</span>=<span class="html__attr_value" style="color: maroon;">"reg"</span> <span class="html__attr_name" style="color: green;">action</span>=<span class="html__attr_value" style="color: maroon;">"register.php"</span> <span class="html__attr_name" style="color: green;">method</span>=<span class="html__attr_value" style="color: maroon;">"post"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span> <span class="html__tag_start" style="font-weight: bold; color: navy;"><p</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span>用户名:<span class="html__tag_start" style="font-weight: bold; color: navy;"><input</span> <span class="html__attr_name" style="color: green;">type</span>=<span class="html__attr_value" style="color: maroon;">"text"</span> <span class="html__attr_name" style="color: green;">class</span>=<span class="html__attr_value" style="color: maroon;">"input"</span> <span class="html__attr_name" style="color: green;">name</span>=<span class="html__attr_value" style="color: maroon;">"username"</span> <span class="html__attr_name" style="color: green;">id</span>=<span class="html__attr_value" style="color: maroon;">"user"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_end" style="color: navy;"></p></span> <span class="html__tag_start" style="font-weight: bold; color: navy;"><p</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span>密 码:<span class="html__tag_start" style="font-weight: bold; color: navy;"><input</span> <span class="html__attr_name" style="color: green;">type</span>=<span class="html__attr_value" style="color: maroon;">"password"</span> <span class="html__attr_name" style="color: green;">class</span>=<span class="html__attr_value" style="color: maroon;">"input"</span> <span class="html__attr_name" style="color: green;">name</span>=<span class="html__attr_value" style="color: maroon;">"password"</span> <span class="html__attr_name" style="color: green;">id</span>=<span class="html__attr_value" style="color: maroon;">"pass"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_end" style="color: navy;"></p></span> <span class="html__tag_start" style="font-weight: bold; color: navy;"><p</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span>E-mail:<span class="html__tag_start" style="font-weight: bold; color: navy;"><input</span> <span class="html__attr_name" style="color: green;">type</span>=<span class="html__attr_value" style="color: maroon;">"text"</span> <span class="html__attr_name" style="color: green;">class</span>=<span class="html__attr_value" style="color: maroon;">"input"</span> <span class="html__attr_name" style="color: green;">name</span>=<span class="html__attr_value" style="color: maroon;">"email"</span> <span class="html__attr_name" style="color: green;">id</span>=<span class="html__attr_value" style="color: maroon;">"email"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_end" style="color: navy;"></p></span> <span class="html__tag_start" style="font-weight: bold; color: navy;"><p</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_start" style="font-weight: bold; color: navy;"><input</span> <span class="html__attr_name" style="color: green;">type</span>=<span class="html__attr_value" style="color: maroon;">"submit"</span> <span class="html__attr_name" style="color: green;">class</span>=<span class="html__attr_value" style="color: maroon;">"btn"</span> <span class="html__attr_name" style="color: green;">value</span>=<span class="html__attr_value" style="color: maroon;">"提交注册"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_end" style="color: navy;"></p></span> <span class="html__tag_end" style="color: navy;"></form></span> |
对于用户的输入要进行必要的前端验证,关于表单验证功能,建议您参考本站文章:实例讲解表单验证插件Validation的应用,本文对前端验证代码略过,另外其实页面中还应该有个要求用户重复输入密码的输入框,一时偷懒就此略过。
register.php
用户将注册信息提交到register.php进行处理。register.php需要完成写入数据和发送邮件两大功能。
首先包含必要的两个文件,connect.php和smtp.class.php,这两个文件在外面提供的下载包里有,欢迎下载。
1 2 3 4 |
<span class="php__keyword" style="font-weight: bold; color: navy;">include_once</span>(<span class="php__string2" style="color: fuchsia;">"connect.php"</span>);<span class="php__com" style="color: green;">//连接数据库</span> <span class="php__keyword" style="font-weight: bold; color: navy;">include_once</span>(<span class="php__string2" style="color: fuchsia;">"smtp.class.php"</span>);<span class="php__com" style="color: green;">//邮件发送类</span> |
然后我们要过滤用户提交的信息,并验证用户名是否存在(前端也可以验证)。
1 2 3 4 5 6 7 8 9 |
<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">username</span> = stripslashes(trim(<span class="php__global" style="color: red;">$_POST</span>[<span class="php__string1" style="color: purple;">'username'</span>])); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">query</span> = mysql_query(<span class="php__string2" style="color: fuchsia;">"select id from t_user where username='$username'"</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">num</span> = mysql_num_rows(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">query</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">num</span>==<span class="php__number" style="color: red;">1</span>){ <span class="php__keyword" style="font-weight: bold; color: navy;">echo</span> <span class="php__string1" style="color: purple;">'用户名已存在,请换个其他的用户名'</span>; <span class="php__keyword" style="font-weight: bold; color: navy;">exit</span>; } |
接着我们将用户密码加密,构造激活识别码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">password</span> = md5(trim(<span class="php__global" style="color: red;">$_POST</span>[<span class="php__string1" style="color: purple;">'password'</span>])); <span class="php__com" style="color: green;">//加密密码</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span> = trim(<span class="php__global" style="color: red;">$_POST</span>[<span class="php__string1" style="color: purple;">'email'</span>]); <span class="php__com" style="color: green;">//邮箱</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">regtime</span> = time(); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token</span> = md5(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">username</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">password</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">regtime</span>); <span class="php__com" style="color: green;">//创建用于激活识别码</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token_exptime</span> = time()+<span class="php__number" style="color: red;">60</span>*<span class="php__number" style="color: red;">60</span>*<span class="php__number" style="color: red;">24</span>;<span class="php__com" style="color: green;">//过期时间为24小时后</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">sql</span> = <span class="php__string2" style="color: fuchsia;">"insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`) values ('$username','$password','$email','$token','$token_exptime','$regtime')"</span>; mysql_query(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">sql</span>); |
上述代码中,$token即构造好的激活识别码,它是由用户名、密码和当前时间组成并md5加密得来的。$token_exptime用于设置激活链接URL的过期时间,用户在这个时间段内可以激活帐号,本例设置的是24小时内激活有效。最后将这些字段插入到数据表t_user中。
当数据插入成功后,调用邮件发送类将激活信息发送给用户注册的邮箱,注意将构造好的激活识别码组成一个完整的URL作为用户点击时的激活链接,以下是详细代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(mysql_insert_id()){ <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpserver</span> = <span class="php__string2" style="color: fuchsia;">""</span>; <span class="php__com" style="color: green;">//SMTP服务器,如:smtp.163.com</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpserverport</span> = <span class="php__number" style="color: red;">25</span>; <span class="php__com" style="color: green;">//SMTP服务器端口,一般为25</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpusermail</span> = <span class="php__string2" style="color: fuchsia;">""</span>; <span class="php__com" style="color: green;">//SMTP服务器的用户邮箱,如xxx@163.com</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpuser</span> = <span class="php__string2" style="color: fuchsia;">""</span>; <span class="php__com" style="color: green;">//SMTP服务器的用户帐号xxx@163.com</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtppass</span> = <span class="php__string2" style="color: fuchsia;">""</span>; <span class="php__com" style="color: green;">//SMTP服务器的用户密码</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtp</span> = <span class="php__keyword" style="font-weight: bold; color: navy;">new</span> Smtp(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpserver</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpserverport</span>, <span class="php__value" style="font-weight: bold; color: gray;">true</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpuser</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtppass</span>); <span class="php__com" style="color: green;">//实例化邮件类</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailtype</span> = <span class="php__string2" style="color: fuchsia;">"HTML"</span>; <span class="php__com" style="color: green;">//信件类型,文本:text;网页:HTML</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpemailto</span> = <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span>; <span class="php__com" style="color: green;">//接收邮件方,本例为注册用户的Email</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpemailfrom</span> = <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpusermail</span>; <span class="php__com" style="color: green;">//发送邮件方,如xxx@163.com</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailsubject</span> = <span class="php__string2" style="color: fuchsia;">"用户帐号激活"</span>;<span class="php__com" style="color: green;">//邮件标题</span> <span class="php__com" style="color: green;">//邮件主体内容</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailbody</span> = <span class="php__string2" style="color: fuchsia;">"亲爱的"</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">username</span>.<span class="php__string2" style="color: fuchsia;">":<br/>感谢您在我站注册了新帐号。<br/>请点击链接激活您的帐号。<br/> <a href='http://www.helloweba.com/demo/register/active.php?verify="</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token</span>.<span class="php__string2" style="color: fuchsia;">"' target= '_blank'>http://www.helloweba.com/demo/register/active.php?verify="</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token</span>.<span class="php__string2" style="color: fuchsia;">"</a><br/> 如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接24小时内有效。"</span>; <span class="php__com" style="color: green;">//发送邮件</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">rs</span> = <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtp</span>->sendmail(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpemailto</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">smtpemailfrom</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailsubject</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailbody</span>, <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailtype</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">rs</span>==<span class="php__number" style="color: red;">1</span>){ <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span> = <span class="php__string1" style="color: purple;">'恭喜您,注册成功!<br/>请登录到您的邮箱及时激活您的帐号!'</span>; }<span class="php__keyword" style="font-weight: bold; color: navy;">else</span>{ <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span> = <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">rs</span>; } } <span class="php__keyword" style="font-weight: bold; color: navy;">echo</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span>; |
还有一个相当好用且强大的邮件发送类分享个大家:使用PHPMailer发送带附件并支持HTML内容的邮件,直接可以用哦。
active.php
如果不出意外,您注册帐号时填写的Email将收到一封helloweba发送的邮件,这个时候您直接点击激活链接,交由active.php处理。
active.php接收提交的链接信息,获取参数verify的值,即激活识别码。将它与数据表中的用户信息进行查询对比,如果有相应的数据集,判断是否过期,如果在有效期内则将对应的用户表中字段status设置1,即已激活,这样就完成了激活功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<span class="php__keyword" style="font-weight: bold; color: navy;">include_once</span>(<span class="php__string2" style="color: fuchsia;">"connect.php"</span>);<span class="php__com" style="color: green;">//连接数据库</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">verify</span> = stripslashes(trim(<span class="php__global" style="color: red;">$_GET</span>[<span class="php__string1" style="color: purple;">'verify'</span>])); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">nowtime</span> = time(); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">query</span> = mysql_query(<span class="php__string2" style="color: fuchsia;">"select id,token_exptime from t_user where status='0' and `token`='$verify'"</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">row</span> = mysql_fetch_array(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">query</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">row</span>){ <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">nowtime</span>><span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">row</span>[<span class="php__string1" style="color: purple;">'token_exptime'</span>]){ <span class="php__com" style="color: green;">//24hour</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span> = <span class="php__string1" style="color: purple;">'您的激活有效期已过,请登录您的帐号重新发送激活邮件.'</span>; }<span class="php__keyword" style="font-weight: bold; color: navy;">else</span>{ mysql_query(<span class="php__string2" style="color: fuchsia;">"update t_user set status=1 where id="</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">row</span>[<span class="php__string1" style="color: purple;">'id'</span>]); <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(mysql_affected_rows(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">link</span>)!=<span class="php__number" style="color: red;">1</span>) <span class="php__keyword" style="font-weight: bold; color: navy;">die</span>(<span class="php__number" style="color: red;">0</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span> = <span class="php__string1" style="color: purple;">'激活成功!'</span>; } }<span class="php__keyword" style="font-weight: bold; color: navy;">else</span>{ <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span> = <span class="php__string1" style="color: purple;">'error.'</span>; } <span class="php__keyword" style="font-weight: bold; color: navy;">echo</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">msg</span>; |
激活成功后,发现token字段并没有用处了,您可以清空。接下来我们会讲解用户找回密码的功能,也要用到邮箱验证,敬请关注。
http://www.helloweba.com/view-blog-228.html
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1163