概述:本文主要展示用户登录退出的后台代码,代码中含有完整注释。
代码:
//登录函数
public function login(){
//判断Session中是否有用户信息
if(Session::has('UserInfo')){
//有则直接跳转到后台首页
return redirect(Url('Index/index'));
}
//判断是否为请求信息
if(Request::isPost()){
//获取前台传过来的用户名
$nickname = Request::param('username');
//获取前台传过来的密码
$password = Request::param('password');
//获取前台传过来的验证码
$captcha = Request::param("captcha");
//如果验证码正确 并且用户名和密码不能为空
if (captcha_check($captcha) == true && $nickname != '' && $password != ''){
//在前台传递过来的密码中加入混淆字符串
$password .= 'www.pcxpcx.com';
//从管理员表中筛选,参数为前台传递过来的用户名及经过混淆和md5加密的密码
$res = Db::name('admin')->where(['nickname' => $nickname, 'pwd' => md5($password)])->find();
//判断筛选结果
if($res != null){
//结果不为空时将用户id写入Session
Session::set('UserInfo',[
'UID'=>$res['id']
]);
//更新管理员表中此用户的登录时间和登录IP
Db::name('admin')->where(['name'=>$res['name']])->update(['login_dt' => date('Y-m-d H:i:s'),'login_ip'=>Request::ip()]);
//跳转到后台首页
return redirect(Url('Index/index'));
}
}
}
return View::fetch();
}
//退出函数
public function logout(){
//清除用户Session
Session::delete('UserInfo');
//跳转到登录页
return redirect(Url('Index/login'));
}
原创不易,转载请保留本站版权。