thinkphp5登陸並保存session、根據不一樣用戶權限跳轉不一樣頁面

本文講述如何在thinkphp5中完成登陸並保存session、而後根據不一樣的用戶權限跳轉相應頁面功能的實現。我也在學習thinkphp源碼的路上,記錄一下並與你們分享。完成該步驟主要有如下三個步驟完成。php


1、密碼校驗thinkphp

這裏view層提交過來的用戶名和密碼是不加密的,數據中的密碼是通過md5加密的,因此首先對密碼進行加密,而後跟數據庫中的記錄比對,若是一致則認爲成功。數據庫


2、session保存session

若是校驗成功則將用戶信息保存在session中。thinkphp5


3、根據不一樣權限跳轉post

有時候咱們對於不一樣的用戶展現的頁面也不一樣,這時就須要咱們根據用戶的權限跳轉到相應的頁面。學習


4、實現代碼this

 1 // 登陸
 2 public function login()
 3 {
 4     //密碼加密並從數據庫查找記錄
 5     $map['username'] = input('post.a');
 6     $map['password'] = md5(input('post.b'));
 7     $user=db('user')->where($where)->find();
 8     //驗證成功則保存session
 9     if ($user) {
10         unset($user["psd"]);
11         session("user", $user['id']);
12         //根據不一樣權限跳轉
13         if($user['quanxian'] == 0){
14             $this->redirect('Module1/index/index');
15         }
16         elseif ($user['quanxian'] == 1) {
17           $this->redirect('MOdule2/index/index');
18         }
19         else{
20           $this->redirect('Module3/index/index');
21         }
22     }else{
23         print_r ('error!');
24         return false;
25     }
26 }
相關文章
相關標籤/搜索