????:<?php namespace app\admin\controller; use app\admin\model\UserModel; use think\Controller; use think\facade\Request; use think\facade\Session; class Login extend
<?php namespace app\admin\controller; use app\admin\model\UserModel; use think\Controller; use think\facade\Request; use think\facade\Session; class Login extends Controller{ public function login(){ //渲染登錄頁面 return $this->fetch(); } //登陸驗證的方法 public function DoLogin(){ //獲取前臺提交的數(shù)據(jù) $data = Request::param(); $username = $data['username']; //使用username變量作為查詢條件,到數(shù)據(jù)庫中查詢用戶名對應的數(shù)據(jù) $user = UserModel::where('username',$username)->find(); if($user != true){ $info = ['res'=>0,'msg'=>'用戶名不存在!']; } elseif($data['password'] != $user['password']){ //如果查詢到數(shù)據(jù),驗證一下密碼 $info = ['res'=>0,'msg'=>'密碼錯誤!']; } else{ $info = ['res'=>1,'msg'=>'登錄成功']; //存儲登錄信息 Session::set('username',$user['username']); } return $info; } //在登錄控制器中再寫一個退出方法 public function LoginOut(){ //刪除Session中的用戶名 Session::delete('username'); //跳轉回登陸頁面 $this->redirect('login'); } }
?? ???:天蓬老師?? ??:2019-04-12 09:19:54
???? ??:使用session記錄用戶信息, 其實比cookie要安全的多, 但本質上還是基于cookie的, 這個要注意