您的位置:首页 > 编程语言 > PHP开发

php 里邮箱找回密码 的正确格式

2016-07-21 20:23 585 查看
原理:

用户找回密码的时候,填写用户名,程序得到用户名便可以去数据库取出用户对应的密码以及当时填写的邮箱,

根据用户名和密码生成一个key=md5(username+password),然后$string=base64_encode(username+key),发送邮件给用户,邮件内容为http://www.xxx.com?p=$string,

用户点击链接地址,程序进行相关操作,先$str=base64_decode($string),之后$arr=explode('+',$str),$arr[0]肯定为用户名,根据用户名得到用户的密码,再使用key=md5(username+password),判断$arr[1]与key是否一致,一致的话就输出两个表单,让用户填写新的密码和确认密码。

如何让这个找回密码的链接具有时效性(比如15分钟后失效)?

答:原理:在地址栏上面加上一个时间和这个时间的加密,如果用户点击这个链接去处理的当时时间-地址栏的时间大于15分钟,则这个找回密码的链接失效

http://www.xxx.com?username=xxx&key=$string &code=md5("自己定义的常量串".$time)&time=$time

其中code是用来检验time是否有修改过。

加密格式 : http://localhost/month9/wei_e/web/index.php?r=login/update&key=db1c4115276c5ae169b15979e451ab12&string=YWRtaW4rc2hlbg==&time=MTQ2ODgxNjM3NytzaGVu

$bool = User::find() -> where(['uname'=>$user_data['uname']]) -> orWhere(['email'=>$user_data['uname']]) ->asArray() -> one();
if($bool){
$key = md5($bool['uname']."+".$bool['upwd']);
$string=base64_encode($bool['uname']."+shen");
$now_time=base64_encode(time()."+shen");
$url = substr('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],0,strpos('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],'we'))."wei_e/web/index.php?r=login/update&key=$key&string=$string&time=$now_time";
$bool = Yii::$app->mailer->compose()
->setFrom('m18612193548@163.com')
->setTo($bool['email'])
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody('<b>'.$url.'</b>')
->send();
if($bool){
echo "<script>alert('发送成功');location.href='https://mail.qq.com/'</script>";
}


if(\Yii::$app->request->isGet) {
$update_data = \Yii::$app->request->get();
// print_r($update_data);die;
$str = base64_decode($update_data['string']);
$time = base64_decode($update_data['key']);
$uname = explode('+', $str);
$time = explode('+', $time);
$user_data = User::find()->where(['uname' => $uname])->asArray()->one();
$key = md5($user_data['uname'] . "+" . $user_data['upwd']);
if ($key == $update_data['key'] && time() - $time[0] > 3600) {
return $this->renderPartial('update', ['uname' => $user_data['uname']]);
} else {
echo "<script>alert('链接失效');location.href='index.php?r=login/index'</script>";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: