国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

基于Laravel5.4實(shí)現(xiàn)多字段登錄功能方法示例

2024-05-04 23:59:23
字體:
供稿:網(wǎng)友

前言

最近在一個(gè)項(xiàng)目中需要實(shí)現(xiàn)一個(gè)多字段登錄功能,簡單來說就是可以使用用戶名、郵箱或手機(jī)號任意一種方式進(jìn)行登錄。所以本文就來給大家介紹了關(guān)于Laravel5.4多字段登錄的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說了,來一起看看詳細(xì)的介紹吧。

以下內(nèi)容基于laravel5.4

方法如下:

首先,通過artisan工具生成auth模塊

php artisan make:auth

這時(shí)候App/Http/Controllers目錄下會(huì)新增一個(gè)Auth目錄,該目錄下為注冊登錄相關(guān)的控制器,resources/views目錄下也會(huì)生成一些與注冊登錄相關(guān)的視圖

laravel的官方文檔中說手動(dòng)認(rèn)證用戶需要使用Illuminate/Support/Facades/Auth類的attempt方法,如下:

<?phpnamespace App/Http/Controllers;use Illuminate/Support/Facades/Auth;class LoginController extends Controller{ /**  * Handle an authentication attempt.  *  * @return Response  */ public function authenticate() {  if (Auth::attempt(['email' => $email, 'password' => $password])) {   // Authentication passed...   return redirect()->intended('dashboard');  } }}

這個(gè)方法會(huì)根據(jù)你傳入的參數(shù)判斷數(shù)據(jù)庫中是否存在與之相匹配的用戶,如果存在并且密碼正確返回true,反之返回false

遂在LoginController中添加該方法,但是好像并沒有效果

于是開始觀察LoginController的實(shí)現(xiàn)機(jī)制,發(fā)現(xiàn)它實(shí)現(xiàn)了一個(gè)AuthenticatesUsers的trait,追蹤到這個(gè)trait的定義文件,發(fā)現(xiàn)這個(gè)文件就是我們想要的東西

里面有一個(gè)login方法,就是負(fù)責(zé)處理登錄的邏輯

/**  * Handle a login request to the application.  *  * @param /Illuminate/Http/Request $request  * @return /Illuminate/Http/RedirectResponse|/Illuminate/Http/Response  */ public function login(Request $request) {  // 表單驗(yàn)證  $this->validateLogin($request);  // If the class is using the ThrottlesLogins trait, we can automatically throttle  // the login attempts for this application. We'll key this by the username and  // the IP address of the client making these requests into this application.  // 防止暴力破解,多次登錄失敗會(huì)根據(jù)IP鎖定  if ($this->hasTooManyLoginAttempts($request)) {   $this->fireLockoutEvent($request);   return $this->sendLockoutResponse($request);  }    // 這個(gè)就是主要的負(fù)責(zé)判斷數(shù)據(jù)庫中是否存在相應(yīng)的賬號和密碼的地方,我們需要重寫的就是attemptLogin方法  if ($this->attemptLogin($request)) {   return $this->sendLoginResponse($request);  }  // If the login attempt was unsuccessful we will increment the number of attempts  // to login and redirect the user back to the login form. Of course, when this  // user surpasses their maximum number of attempts they will get locked out.  // 登錄失敗,失敗次數(shù)++,防止暴力破解  $this->incrementLoginAttempts($request);  // 返回失敗響應(yīng)  return $this->sendFailedLoginResponse($request); }

分析了一波這個(gè)文件,發(fā)現(xiàn)主要進(jìn)行登錄判斷的就是attemptLogin方法,我們只要重寫這個(gè)方法即可,先看看原來的是怎么寫的,根據(jù)原來的進(jìn)行重寫:

/**  * Attempt to log the user into the application.  *  * @param /Illuminate/Http/Request $request  * @return bool  */ protected function attemptLogin(Request $request) {  return $this->guard()->attempt(   $this->credentials($request), $request->has('remember')  ); }

在LoginController重寫后:

public function attemptLogin(Request $request) {  $username = $request->input('username');  $password = $request->input('password');  // 驗(yàn)證用戶名登錄方式  $usernameLogin = $this->guard()->attempt(   ['username' => $username, 'password' => $password], $request->has('remember')  );  if ($usernameLogin) {   return true;  }  // 驗(yàn)證手機(jī)號登錄方式  $mobileLogin = $this->guard()->attempt(   ['mobile' => $username, 'password' => $password], $request->has('remember')  );  if ($mobileLogin) {   return true;  }  // 驗(yàn)證郵箱登錄方式  $emailLogin = $this->guard()->attempt(   ['email' => $username, 'password' => $password], $request->has('remember')  );  if ($emailLogin) {   return true;  }  return false; }

只需要用attempt方法進(jìn)行多次判斷即可,只要成功就返回true,不成功繼續(xù)用其他字段進(jìn)行判斷,都不成功則返回flase

測試,可以實(shí)現(xiàn)多字段登錄效果

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 禹州市| 灌阳县| 绥宁县| 邵东县| 轮台县| 克拉玛依市| 仪陇县| 宁蒗| 岑巩县| 沛县| 温州市| 南汇区| 绍兴市| 博客| 平南县| 红河县| 渭南市| 紫阳县| 屏山县| 河池市| 鲁山县| 冀州市| 沅陵县| 青冈县| 屯门区| 广西| 阿合奇县| 紫阳县| 金昌市| 乐亭县| 公安县| 连南| 当雄县| 偏关县| 南京市| 汉沽区| 民丰县| 醴陵市| 同江市| 黔西| 临泉县|