本文講述如何在thinkphp5中完成登錄並保存session、然后根據不同的用戶權限跳轉相應頁面功能的實現。我也在學習thinkphp源碼的路上,記錄一下並與大家分享。完成該步驟主要有以下三個步驟完成。
一、密碼校驗
這里view層提交過來的用戶名和密碼是不加密的,數據中的密碼是經過md5加密的,所以首先對密碼進行加密,然后跟數據庫中的記錄比對,如果一致則認為成功。
二、session保存
如果校驗成功則將用戶信息保存在session中。
三、根據不同權限跳轉
有時候我們對於不同的用戶展示的頁面也不同,這時就需要我們根據用戶的權限跳轉到相應的頁面。
資源網站大全 https://55wd.com 我的007辦公資源網站 https://www.wode007.com
四、實現代碼
// 登錄 public function login() { //密碼加密並從數據庫查找記錄 $map['username'] = input('post.a'); $map['password'] = md5(input('post.b')); $user=db('user')->where($where)->find(); //驗證成功則保存session if ($user) { unset($user["psd"]); session("user", $user['id']); //根據不同權限跳轉 if($user['quanxian'] == 0){ $this->redirect('Module1/index/index'); } elseif ($user['quanxian'] == 1) { $this->redirect('MOdule2/index/index'); } else{ $this->redirect('Module3/index/index'); } }else{ print_r ('error!'); return false; } }
來自:https://www.cnblogs.com/woleyia/p/10127142.html