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

原理:

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

根据用户名和密码生成一个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>";
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yilukuangpao

你的鼓励是我创造最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值