-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
87 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
## 邮箱异步验证 | ||
## 邮箱注册 | ||
|
||
- https://segmentfault.com/a/1190000011732910 | ||
|
||
## js验证几个注意点 | ||
|
||
- ``verbose: false``,代表js验证合法后再异步后台验证,这样减少服务器压力 | ||
- `` data: {}`` ,默认传递的就是输入框的值,所以一般不用写该属性,或者为空即可 | ||
|
||
## 后台注意点 | ||
> 参考博客【http://blog.csdn.net/u012995856/article/details/70196562】 | ||
1. ``composer require phpmailer/phpmailer`` | ||
2. 在common.php的创建发送邮箱的公共函数 | ||
3. 在注册register和confirm中进行发送邮件 | ||
|
||
- 注意不是return而是echo | ||
- 返回json格式`` {'valid':true[,'message':'验证成功']}`` | ||
## 邮箱注册异步验证唯一 | ||
|
||
> 参考博客【https://segmentfault.com/a/1190000011732910】 | ||
## 修改Users模型的login()方法 | ||
|
||
``` | ||
//如果邮件未激活 | ||
if(!$user['confirmed']){ | ||
session('unconfirmed',$user['id']); | ||
return -2 ; | ||
} | ||
``` | ||
|
||
## 修改Auth的login()方法 | ||
|
||
``` | ||
//如果未激活跳转到一个unconfirmed.html,可以点击再次发送一封邮箱,可以防止第一封没收到 | ||
if($res == -2){ | ||
return view('unconfirmed'); | ||
} | ||
``` | ||
## 在Auth控制器添加新方法 | ||
|
||
- register:注册成功后发送邮件,如果不是post提交显示注册表单进行注册用户 | ||
|
||
- ajax_email和ajax_username分别是异步验证邮箱和用户名唯一 | ||
|
||
- active_user是点击邮件激活用户,思路:解码出注册时间和邮箱,然后判断是否过期 | ||
|
||
- confirm是进行再次发送一封邮件,confirmed==0代表未激活,取出session的unconfirmed, | ||
最后清除session,防止手动链接的方式进入该方法 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,11 @@ public function login(Request $request) | |
} | ||
$user = new Users(); | ||
$res = $user->login($data['email'], $data['password'], isset($data['remember_me'])? $data['remember_me']:'n'); | ||
if($res > 0){ | ||
//如果未激活跳转到一个unconfirmed.html,可以点击再次发送一封邮箱,可以防止第一封没收到 | ||
if($res == -2){ | ||
return view('unconfirmed'); | ||
} | ||
if($res == 1){ | ||
$this->redirect('/'); | ||
} | ||
$flashed_messages[] = '邮箱或密码错误'; | ||
|
@@ -40,11 +44,12 @@ public function register(Request $request){ | |
$this->assign('flashed_messages',$flashed_messages); | ||
return view('register'); | ||
} | ||
|
||
$user = model('Users'); | ||
$user->data([ | ||
'username'=>$data['username'], | ||
'email'=>$data['email'], | ||
'password'=>$data['password'], | ||
'password'=>md5($data['password']), | ||
'create_time'=>date('Y-m-d H:i:s') | ||
]); | ||
$res = $user->save(); | ||
|
@@ -53,7 +58,7 @@ public function register(Request $request){ | |
//激活密令 | ||
$activecode = encryption($token); | ||
//收件人邮箱 | ||
$toemail='[email protected]'; | ||
$toemail=$data['email']; | ||
//发件人昵称 | ||
$name='php博客'; | ||
//邮件标题 | ||
|
@@ -62,7 +67,8 @@ public function register(Request $request){ | |
$content="<h1>恭喜你,注册成功。</h1><a href=".$request->domain()."/active_user/$activecode>点击激活</a>"; | ||
//如果页面打印bool(true)则发送成功 | ||
if(send_mail($toemail,$name,$subject,$content)){ | ||
$this->success('注册成功,跳转首页...','/login'); | ||
$flashed_messages[] = '注册成功,请登陆邮箱激活'; | ||
$this->assign('flashed_messages',$flashed_messages); | ||
} | ||
}else{ | ||
$flashed_messages[] = '注册失败'; | ||
|
@@ -104,4 +110,29 @@ public function active_user($activecode){ | |
|
||
$this->error('验证密令错误或过期,请重新登陆'); | ||
} | ||
|
||
//再次发送激活邮件 | ||
public function confirm(Request $request){ | ||
if(session('?unconfirmed')){ | ||
$data = Users::get(session('unconfirmed')); | ||
$token = time().'|'.$data['email']; | ||
//激活密令 | ||
$activecode = encryption($token); | ||
//收件人邮箱 | ||
$toemail=$data['email']; | ||
//发件人昵称 | ||
$name='php博客'; | ||
//邮件标题 | ||
$subject='请激活您的账户'; | ||
//邮件内容 | ||
$content="<h1>恭喜你,注册成功。</h1><a href=".$request->domain()."/active_user/$activecode>点击激活</a>"; | ||
if(send_mail($toemail,$name,$subject,$content)){ | ||
session('unconfirmed',null); | ||
$flashed_messages[] = '一封新邮件已经发送'; | ||
$this->assign('flashed_messages',$flashed_messages); | ||
return view('unconfirmed'); | ||
} | ||
} | ||
abort(404); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{extend name="common/base" /} | ||
{block name="title"}博客登陆{/block} | ||
{block name="page_content"} | ||
<br/><br/> | ||
邮箱还未激活,请打开邮箱激活,如果没有收到邮件<a href="/confirm">点击重新发送</a> | ||
{/block} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters