登录事件的绑定 [ 2.0 版本 ]
1.在web.php文件中的‘user’配置中如下:
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'on afterLogin'=>function($event){
$user=$event->identity;
$model=new Log_Login();
$model->USER_ID=$user->USER_ID;
$model->USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
$model->IP=$_SERVER['REMOTE_ADDR'];
$model->LOGIN=date("Y-m-d H:i:s");
$model->save();
$_SESSION['log_id']=$model->ID;
},
'on beforeLogout'=>function ($event){
//$user=$event->identity;
$model = Log_Login::findOne((int)$_SESSION['log_id']);
$model->LOGOUT=date("Y-m-d H:i:s");
$model->update();
}
]
suzy
注册时间:2015-09-09
最后登录:2016-04-22
在线时长:12小时32分
最后登录:2016-04-22
在线时长:12小时32分
- 粉丝1
- 金钱135
- 威望30
- 积分555
共 1 条评论
`'user' => [
'identityClass' => 'app\models\User', 'enableAutoLogin' => true, 'on afterLogin'=>function($event){ $user=$event->identity; $model=new Log_Login(); $model->USER_ID=$user->USER_ID; $model->USER_AGENT=$_SERVER['HTTP_USER_AGENT']; $model->IP=$_SERVER['REMOTE_ADDR']; $model->LOGIN=date("Y-m-d H:i:s"); $model->save(); $_SESSION['log_id']=$model->ID; }, 'on beforeLogout'=>function ($event){ //$user=$event->identity; $model = Log_Login::findOne((int)$_SESSION['log_id']); $model->LOGOUT=date("Y-m-d H:i:s"); $model->update(); }
]`