PHP+Mysql+jQuery找回密码
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
通常所说的密码找回功能不是真的能把忘记的密码找回,因为我们的密码是加密保存的,一般开发者会在验证用户信息后通过程序生成一个新密码或者生成一个特定的链接并发送邮件到用户邮箱,用户从邮箱链接到网站的重置密码模块重新设置新密码。
当然现在有的网站也有手机短信的方式找回密码,原理就是通过发送验证码来验明正身,和发送邮件验证一样,最终还是要通过重置密码来完成找回密码的流程。
本文将使用PHP+Mysql+jQuery来实现一个密码找回的功能,一般步骤是:
1.表单输入注册时的邮箱;
2.验证用户邮箱是否正确,如果用户邮箱不存在网站的用户表中,则提示用户邮箱未注册;
3.发送邮件,如果用户邮箱确实存在用户表中,则组合用于验证用户信息的字符串,并构造URL发送到用户邮箱中;
4.用户登录邮箱收取邮件,点击URL链接到网站验证程序;
5.网站程序通过用户请求的字符串查询本地用户表,比对用户信息是否正确;
6.如果正确则转到重置密码页面重新设置新密码,反之则提示用户验证无效。
HTML
我们在找回密码的页面上放置一个要求用户输入注册时所用的邮箱,然后提交前台js来处理交互。
1 2 3 4 5 |
<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;"><strong</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span>输入您注册的电子邮箱,找回密码:<span class="html__tag_end" style="color: navy;"></strong></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;">"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_start" style="font-weight: bold; color: navy;"><span</span> <span class="html__attr_name" style="color: green;">id</span>=<span class="html__attr_value" style="color: maroon;">"chkmsg"</span><span class="html__tag_start" style="font-weight: bold; color: navy;">></span><span class="html__tag_end" style="color: navy;"></span></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;">"button"</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;">id</span>=<span class="html__attr_value" style="color: maroon;">"sub_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> |
jQuery
当用户输入完邮箱并点击提交后,jQuery先验证邮箱格式是否正确,如果正确则通过向后台sendmail.php发送Ajax请求,sendmail.php负责验证邮箱是否存在和发送邮件,并会返回相应的处理结果给前台页面,请看jQuery代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$(<span class="js__operator" style="font-weight: bold; color: #4169e1;">function</span>()<span class="js__brace" style="font-weight: bold; color: red;">{</span> $(<span class="js__string" style="color: teal;">"#sub_btn"</span>).click(<span class="js__operator" style="font-weight: bold; color: #4169e1;">function</span>()<span class="js__brace" style="font-weight: bold; color: red;">{</span> <span class="js__statement" style="font-weight: bold; color: navy;">var</span> email = $(<span class="js__string" style="color: teal;">"#email"</span>).val(); <span class="js__statement" style="font-weight: bold; color: navy;">var</span> preg = <span class="js__reg_exp" style="color: maroon;">/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/</span>; <span class="js__sl_comment" style="color: green;">//匹配Email</span> <span class="js__statement" style="font-weight: bold; color: navy;">if</span>(email==<span class="js__string" style="color: teal;">''</span> || !preg.test(email))<span class="js__brace" style="font-weight: bold; color: red;">{</span> $(<span class="js__string" style="color: teal;">"#chkmsg"</span>).html(<span class="js__string" style="color: teal;">"请填写正确的邮箱!"</span>); <span class="js__brace" style="font-weight: bold; color: red;">}</span><span class="js__statement" style="font-weight: bold; color: navy;">else</span><span class="js__brace" style="font-weight: bold; color: red;">{</span> $(<span class="js__string" style="color: teal;">"#sub_btn"</span>).attr(<span class="js__string" style="color: teal;">"disabled"</span>,<span class="js__string" style="color: teal;">"disabled"</span>).val(<span class="js__string" style="color: teal;">'提交中..'</span>).css(<span class="js__string" style="color: teal;">"cursor"</span>,<span class="js__string" style="color: teal;">"default"</span>); $.post(<span class="js__string" style="color: teal;">"sendmail.php"</span>,<span class="js__brace" style="font-weight: bold; color: red;">{</span>mail:email<span class="js__brace" style="font-weight: bold; color: red;">}</span>,<span class="js__operator" style="font-weight: bold; color: #4169e1;">function</span>(msg)<span class="js__brace" style="font-weight: bold; color: red;">{</span> <span class="js__statement" style="font-weight: bold; color: navy;">if</span>(msg==<span class="js__string" style="color: teal;">"noreg"</span>)<span class="js__brace" style="font-weight: bold; color: red;">{</span> $(<span class="js__string" style="color: teal;">"#chkmsg"</span>).html(<span class="js__string" style="color: teal;">"该邮箱尚未注册!"</span>); $(<span class="js__string" style="color: teal;">"#sub_btn"</span>).removeAttr(<span class="js__string" style="color: teal;">"disabled"</span>).val(<span class="js__string" style="color: teal;">'提 交'</span>).css(<span class="js__string" style="color: teal;">"cursor"</span>,<span class="js__string" style="color: teal;">"pointer"</span>); <span class="js__brace" style="font-weight: bold; color: red;">}</span><span class="js__statement" style="font-weight: bold; color: navy;">else</span><span class="js__brace" style="font-weight: bold; color: red;">{</span> $(<span class="js__string" style="color: teal;">".demo"</span>).html(<span class="js__string" style="color: teal;">"<h3>"</span>+msg+<span class="js__string" style="color: teal;">"</h3>"</span>); <span class="js__brace" style="font-weight: bold; color: red;">}</span> <span class="js__brace" style="font-weight: bold; color: red;">}</span>); <span class="js__brace" style="font-weight: bold; color: red;">}</span> <span class="js__brace" style="font-weight: bold; color: red;">}</span>); <span class="js__brace" style="font-weight: bold; color: red;">}</span>) |
以上使用的jQuery代码很方便简洁的完成了前端交互操作,如果您有一定的jQuery基础,那上面的代码一目了然,不多解释。
当然别忘了在页面中加载jQuery库文件,有的同学经常问我说从helloweba.com下载了demo怎么用不了,那80%是jquery或者其他文件加载路径错了导致没加载必要的文件。
PHP
sendmail.php需要验证Email是否存在系统用户表中,如果有,则读取用户信息,将用户id、用户名和密码惊醒md5加密生成一个特别的字符串作为找回密码的验证码,然后构造URL。同时我们为了控制URL链接的时效性,将记录用户提交找回密码动作的操作时间,最后调用邮件发送类发送邮件到用户邮箱,发送邮件类smtp.class.php已经打包好,请下载。
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<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;">email</span> = stripslashes(trim(<span class="php__global" style="color: red;">$_POST</span>[<span class="php__string1" style="color: purple;">'mail'</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;">"select id,username,password from `t_user` where `email`='$email'"</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__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">sql</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;">0</span>){<span class="php__com" style="color: green;">//该邮箱尚未注册!</span> <span class="php__keyword" style="font-weight: bold; color: navy;">echo</span> <span class="php__string1" style="color: purple;">'noreg'</span>; <span class="php__keyword" style="font-weight: bold; color: navy;">exit</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;">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;">$</span><span class="php__variable" style="color: #4040c2;">getpasstime</span> = time(); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">uid</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;">$</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;">uid</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;">'username'</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;">'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;">url</span> = <span class="php__string2" style="color: fuchsia;">"http://www.helloweba.com/demo/resetpass/reset.php?email="</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span>.<span class="php__string2" style="color: fuchsia;">" &token="</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token</span>;<span class="php__com" style="color: green;">//构造URL</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">time</span> = date(<span class="php__string1" style="color: purple;">'Y-m-d H:i'</span>); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">result</span> = sendmail(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">time</span>,<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span>,<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">url</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;">result</span>==<span class="php__number" style="color: red;">1</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;">msg</span> = <span class="php__string1" style="color: purple;">'系统已向您的邮箱发送了一封邮件<br/>请登录到您的邮箱及时重置您的密码!'</span>; <span class="php__com" style="color: green;">//更新数据发送时间</span> mysql_query(<span class="php__string2" style="color: fuchsia;">"update `t_user` set `getpasstime`='$getpasstime' where id='$uid '"</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;">result</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>; } <span class="php__com" style="color: green;">//发送邮件</span> <span class="php__keyword" style="font-weight: bold; color: navy;">function</span> sendmail(<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">time</span>,<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span>,<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">url</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__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服务器端口</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服务器的用户邮箱</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服务器的用户帐号</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;">//这里面的一个true是表示使用身份验证,否则不使用身份验证.</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__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__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">emailsubject</span> = <span class="php__string2" style="color: fuchsia;">"Helloweba.com - 找回密码"</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;">email</span>.<span class="php__string2" style="color: fuchsia;">":<br/>您在"</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">time</span>.<span class="php__string2" style="color: fuchsia;">"提交了找回密码请求。请点击下面的链接重置密码 (按钮24小时内有效)。<br/><a href='"</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">url</span>.<span class="php__string2" style="color: fuchsia;">"'target='_blank'>"</span>.<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">url</span>.<span class="php__string2" style="color: fuchsia;">"</a>"</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;">return</span> <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">rs</span>; } |
好了,这个时候你的邮箱将会收到一封来自helloweba的密码找回邮件,邮件内容中有一个URL链接,点击该链接到helloweba.com的reset.php来验证邮箱。
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 |
<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;">token</span> = stripslashes(trim(<span class="php__global" style="color: red;">$_GET</span>[<span class="php__string1" style="color: purple;">'token'</span>])); <span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">email</span> = stripslashes(trim(<span class="php__global" style="color: red;">$_GET</span>[<span class="php__string1" style="color: purple;">'email'</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;">"select * from `t_user` where email='$email'"</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__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">sql</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;">$</span><span class="php__variable" style="color: #4040c2;">mt</span> = md5(<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;">$</span><span class="php__variable" style="color: #4040c2;">row</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;">row</span>[<span class="php__string1" style="color: purple;">'password'</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;">mt</span>==<span class="php__keyword" style="font-weight: bold; color: navy;">$</span><span class="php__variable" style="color: #4040c2;">token</span>){ <span class="php__keyword" style="font-weight: bold; color: navy;">if</span>(time()-<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;">'getpasstime'</span>]><span class="php__number" style="color: red;">24</span>*<span class="php__number" style="color: red;">60</span>*<span class="php__number" style="color: red;">60</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__com" style="color: green;">//重置密码...</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__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;">'错误的链接!'</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>; |
reset.php首先接受参数email和token,然后根据email查询数据表t_user中是否存在该Email,如果存在则获取该用户的信息,并且和sendmail.php中的token组合方式一样构建token值,然后与url传过来的token进行对比,如果当前时间与发送邮件时的时间相差超过24小时的,则提示“该链接已过期!”,反之,则说明链接有效,并且调转到重置密码页面,最后就是用户自己设置新密码了。
小结:通过注册邮箱验证与本文邮件找回密码,我们知道发送邮件在网站开发中的应用以及它的重要性,当然,现在也流行短信验证应用,这个需要相关的短信接口对接就可以了。
最后,附上数据表t_user结构:
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE `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>, `password` varchar(<span class="php__number" style="color: red;">32</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span>, `email` varchar(<span class="php__number" style="color: red;">50</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span>, `getpasstime` int(<span class="php__number" style="color: red;">10</span>) NOT <span class="php__value" style="font-weight: bold; color: gray;">NULL</span>, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; http://www.helloweba.com/view-blog-229.html |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1165