基于Laravel5.4實(shí)現(xiàn)多字段登錄功能方法示例
前言
最近在一個(gè)項(xiàng)目中需要實(shí)現(xiàn)一個(gè)多字段登錄功能,簡(jiǎn)單來(lái)說(shuō)就是可以使用用戶(hù)名、郵箱或手機(jī)號(hào)任意一種方式進(jìn)行登錄。所以本文就來(lái)給大家介紹了關(guān)于Laravel5.4多字段登錄的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
以下內(nèi)容基于laravel5.4
方法如下:
首先,通過(guò)artisan工具生成auth模塊
php artisan make:auth
這時(shí)候App\Http\Controllers目錄下會(huì)新增一個(gè)Auth目錄,該目錄下為注冊(cè)登錄相關(guān)的控制器,resources\views目錄下也會(huì)生成一些與注冊(cè)登錄相關(guān)的視圖
laravel的官方文檔中說(shuō)手動(dòng)認(rèn)證用戶(hù)需要使用Illuminate\Support\Facades\Auth類(lèi)的attempt方法,如下:
<?php
namespace 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ù)庫(kù)中是否存在與之相匹配的用戶(hù),如果存在并且密碼正確返回true,反之返回false
遂在LoginController中添加該方法,但是好像并沒(méi)有效果
于是開(kāi)始觀(guān)察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ù)庫(kù)中是否存在相應(yīng)的賬號(hào)和密碼的地方,我們需要重寫(xiě)的就是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方法,我們只要重寫(xiě)這個(gè)方法即可,先看看原來(lái)的是怎么寫(xiě)的,根據(jù)原來(lái)的進(jìn)行重寫(xiě):
/**
* 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重寫(xiě)后:
public function attemptLogin(Request $request)
{
$username = $request->input('username');
$password = $request->input('password');
// 驗(yàn)證用戶(hù)名登錄方式
$usernameLogin = $this->guard()->attempt(
['username' => $username, 'password' => $password], $request->has('remember')
);
if ($usernameLogin) {
return true;
}
// 驗(yàn)證手機(jī)號(hào)登錄方式
$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
測(cè)試,可以實(shí)現(xiàn)多字段登錄效果
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Laravel 5.4重新登錄實(shí)現(xiàn)跳轉(zhuǎn)到登錄前頁(yè)面的原理和方法
- Laravel5.2使用Captcha生成驗(yàn)證碼實(shí)現(xiàn)登錄(session巨坑)
- laravel5.2實(shí)現(xiàn)區(qū)分前后臺(tái)用戶(hù)登錄的方法
- laravel5實(shí)現(xiàn)微信第三方登錄功能
- Laravel5.5 實(shí)現(xiàn)后臺(tái)管理登錄的方法(自定義用戶(hù)表登錄)
- 解決laravel5中auth用戶(hù)登錄其他頁(yè)面獲取不到登錄信息的問(wèn)題
- laravel 5.3 單用戶(hù)登錄簡(jiǎn)單實(shí)現(xiàn)方法
- Laravel5.4框架使用socialite實(shí)現(xiàn)github登錄的方法
- Laravel 5.5 實(shí)現(xiàn)禁用用戶(hù)注冊(cè)示例
- Laravel5.1 框架登錄和注冊(cè)實(shí)現(xiàn)方法詳解
相關(guān)文章
php微信公眾號(hào)開(kāi)發(fā)之圖片回復(fù)
這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)開(kāi)發(fā)之圖片回復(fù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Zend Framework動(dòng)作助手Json用法實(shí)例分析
這篇文章主要介紹了Zend Framework動(dòng)作助手Json用法,結(jié)合實(shí)例形式分析了Zend Framework動(dòng)作助手Json的功能與相關(guān)使用技巧,需要的朋友可以參考下2016-03-03
Yii基于數(shù)組和對(duì)象的Model查詢(xún)技巧實(shí)例詳解
這篇文章主要介紹了Yii基于數(shù)組和對(duì)象的Model查詢(xún)技巧,結(jié)合實(shí)例形式較為詳細(xì)的分析了Yii針對(duì)數(shù)組及對(duì)象的Model查詢(xún)使用技巧,需要的朋友可以參考下2015-12-12
使用laravel的migrate創(chuàng)建數(shù)據(jù)表的方法
今天小編就為大家分享一篇使用laravel的migrate創(chuàng)建數(shù)據(jù)表的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
CentOS下與Apache連接的PHP多版本共存方案實(shí)現(xiàn)詳解
這篇文章主要介紹了CentOS下與Apache連接的PHP多版本共存方案實(shí)現(xiàn),針對(duì)mod_fcgi模塊的配置作了一些說(shuō)明,需要的朋友可以參考下2015-12-12
php分頁(yè)原理 分頁(yè)代碼 分頁(yè)類(lèi)制作教程
這篇文章主要為大家詳細(xì)介紹了php分頁(yè)原理,php分頁(yè)代碼,php分頁(yè)類(lèi)制作教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

