创建时间:2017-08-14
忘记密码 用邮箱来设置新密码
当注册新账号也可以用此方法来验证邮箱
用户表 token用来存激活码,token_exptime用来存激活码的过期时间戳

获取用户提交的邮箱,检查邮箱数据库是否存在,如果邮箱存在
1、设置一个过期时间
$token_exptime = time()+60*60*24*7; //过期时间为7天后
2、设置一个激活码
$email = md5(trim($mail));//trim() 函数移除字符串两侧的空白字符或其他预定义字符。
$token = md5('chenguangan'.$email.time()); //创建用于激活识别码
3、将邮箱作为条件,插入激活码跟过期时间戳进数据库
4、将激活码发送到该邮箱 xxx.com?token=$token
下面ThinkPHP5为例
//找回密码
public function pass(){
if(input("?post.email")){
$mail = input("post.email");//获取用户提交的邮箱
$db = model('member');
$arr = $db->checkEmail($mail);//检测邮箱数据库是否存在
if($arr['code']==200){
$token_exptime = time()+60*60*24*7; //过期时间为7天后
$email = md5(trim($mail));//trim() 函数移除字符串两侧的空白字符或其他预定义字符。
$token = md5('chenguangan'.$email.time()); //创建用于激活识别码
$info = $db->insertToken($token,$token_exptime,$mail);//将邮箱作为条件,插入token跟过期时间戳
if($info['code']==200){
$strBig = "<p style='font-size: 16px;color: #555;text-indent: 2em;font-weight: 600;'>亲爱的:</p>";
$strBig.= "<p style='font-size: 16px;color: #555;text-indent: 2em;'>".$mail."</p>";
$strBig.= "<p style='font-size: 16px;color: #555;'>感谢您在我站注册了帐号,请点击链接修改你的密码</p>";
$strBig.= "<a href='http://www.520anan.com/a/other/up_pass?token=".$token ."' target='_blank' style='font-size: 16px;color: #c00;'> http://www.520anan.com/a/other/up_pass?token=".$token ." </a>";
$strBig.= "<p style='font-size: 16px;color: #555;text-indent: 2em;'>如果以上链接无法点击,请将它复制到你的浏览器地址栏中进入访问,该链接7天内有效。</p>";
$strBig.= "<p style='font-size: 16px;color: #555;text-indent: 2em;'>如果你确认此密码找回功能是你启用的,请点击上面的链接。</p>";
$strBig.= "<p style='font-size: 16px;color: #555;text-indent: 2em;'>".date("Y-m-d H:i:s")."由 <a href='http://www.520anan.com' style='font-size: 16px;color: #c00;'> http://www.520anan.com </a>发出</p>";
$code = SendMail($mail,"找回密码",$strBig);//发送邮件
if($code==200){
return json('发送成功,请登录邮箱进行验证,如果没收到,请到垃圾箱查看');
}else{
return json('发送邮件失败');
}
}else{
return json('发送邮件失败');
}
}else{
return json('邮箱不存在,请注册');
}
}
return $this->fetch();
}用户从邮箱点击链接进来
获取URL的token激活码,数据库匹配检查token是否存在
以及检查token是否过期,否则重定向到本站的404页面
如果token没有问题,为了方便后面使用,可以将token装到cookie并跳转到填写新密码页面
//检查token并跳转
public function up_pass($token){
$db = model('member');
$arr = $db->checkToken($token);//检查token是否存在以及是否过期
if($arr['code']==200){
cookie('token',$token);//将token存入cookie
return $this->fetch();
}else{
$this->redirect('other/error_page');//重定向到404页面
}
}1、让用户在页面上填写需要需要找回密码的 邮箱 以及 新密码
2、数据库查找 token 条件为 邮箱,匹配是否存在
3、如果上述通过,则将新密码插入
//找回密码-修改密码
public function changes_pass(){
if(input('?post.email')&&input('?post.passl')&&(input('post.passl')==input('post.pass2'))){
$db = model('member');
$arr = $db->changePass(input('post.email'),input('post.passl'),cookie('token'));
return json($arr);
}else{
return json(input('post.'));
}
}