用户登录登出 [ 2.0 版本 ]
<?php
namespace app\modules\controllers;
header("content-type:text/html;charset=utf-8");
use yii;
use app\modules\models\Admin;
use yii\web\Controller;
/**
* Default controller for the `admin` module
*/
class AuthController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public $layout = false;
public function actionSignIn()
{
$request = yii::$app->request;
$admin = new Admin(['scenario'=>'login']);
$session = yii::$app->session;
if($request->isPost)
{
$post = $request->post();
// var_dump($post);
if($admin->load($post) && $admin->validate())
{
if($adminInfo = $admin->checkLogin())
{
$leftTime = ($admin->remember) ? 3*24*3600:0;
session_set_cookie_params($leftTime);
//登录成功
$session['admin'] = $adminInfo;
$this->redirect(['default/index']);
}
else
{
//登录失败
return $this->render('signin',['admin'=>$admin]);
}
}
else
{
return $this->render('signin',['admin'=>$admin]);
}
}
else
{
if($session->get('admin'))
{
$this->redirect(['default/index']);
}
return $this->render('signin',['admin'=>$admin]);
}
}
/**
* 管理员退出
*/
public function actionSignOut()
{
//清除SESSION
$session = yii::$app->session;
$session->remove('admin');
$session->destroy();
$this->redirect(['auth/sign-in']);
}
}
1196281550
注册时间:2017-10-17
最后登录:2019-10-28
在线时长:2小时23分
最后登录:2019-10-28
在线时长:2小时23分
- 粉丝3
- 金钱65
- 威望40
- 积分485
共 0 条评论