Skip to content

Commit

Permalink
登陆模块完结
Browse files Browse the repository at this point in the history
  • Loading branch information
mytheshow committed Oct 26, 2017
1 parent 58a2c9b commit 06e524e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
49 changes: 38 additions & 11 deletions README.md
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,防止手动链接的方式进入该方法
2 changes: 1 addition & 1 deletion application/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null
}
}
return $mail->Send() ? true : $mail->ErrorInfo;
}
}
2 changes: 1 addition & 1 deletion application/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// 应用命名空间
'app_namespace' => 'app',
// 应用调试模式
'app_debug' => true,
'app_debug' => false,
// 应用Trace
'app_trace' => false,
// 应用模式状态
Expand Down
39 changes: 35 additions & 4 deletions application/index/controller/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = '邮箱或密码错误';
Expand All @@ -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();
Expand All @@ -53,7 +58,7 @@ public function register(Request $request){
//激活密令
$activecode = encryption($token);
//收件人邮箱
$toemail='[email protected]';
$toemail=$data['email'];
//发件人昵称
$name='php博客';
//邮件标题
Expand All @@ -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[] = '注册失败';
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions application/index/model/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Users extends Model {
public function login($email, $password, $auto){
$user = $this->where('email',$email)->find();
if(md5($password)==$user['password']){
//如果邮件未激活
if(!$user['confirmed']){
session('unconfirmed',$user['id']);
return -2 ;
}
$request = request();
$update = array(
'id'=>$user['id'],
Expand Down
6 changes: 6 additions & 0 deletions application/index/view/auth/unconfirmed.html
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}
1 change: 1 addition & 0 deletions application/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
Route::post('/ajax_email', 'index/auth/ajax_email',['ajax' => true]);
Route::post('/ajax_username', 'index/auth/ajax_username',['ajax' => true]);
Route::get('/active_user/:activecode', 'index/auth/active_user');
Route::get('/confirm', 'index/auth/confirm');

0 comments on commit 06e524e

Please sign in to comment.