abstrait:<?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(){ //渲染登錄頁(yè)面 return $this->fetch(); } //登陸驗(yàn)證的方法 public function DoLogin(){ //獲取前臺(tái)提交的數(shù)據(jù) $data = Request::param(); $username = $data['username']; //使用username變量作為查詢(xún)條件,到數(shù)據(jù)庫(kù)中查詢(xún)用戶(hù)名對(duì)應(yīng)的數(shù)據(jù) $user = UserModel::where('username',$username)->find(); if($user != true){ $info = ['res'=>0,'msg'=>'用戶(hù)名不存在!']; } elseif($data['password'] != $user['password']){ //如果查詢(xún)到數(shù)據(jù),驗(yàn)證一下密碼 $info = ['res'=>0,'msg'=>'密碼錯(cuò)誤!']; } else{ $info = ['res'=>1,'msg'=>'登錄成功']; //存儲(chǔ)登錄信息 Session::set('username',$user['username']); } return $info; } //在登錄控制器中再寫(xiě)一個(gè)退出方法 public function LoginOut(){ //刪除Session中的用戶(hù)名 Session::delete('username'); //跳轉(zhuǎn)回登陸頁(yè)面 $this->redirect('login'); } }
Professeur correcteur:天蓬老師Temps de correction:2019-04-12 09:19:54
Résumé du professeur:使用session記錄用戶(hù)信息, 其實(shí)比cookie要安全的多, 但本質(zhì)上還是基于cookie的, 這個(gè)要注意