加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

基于Laravel5.4实现多字段登录功能方法示例

发布时间:2020-12-14 19:54:29 所属栏目:大数据 来源:网络整理
导读:《PHP实战:基于Laravel5.4实现多字段登录功能方法示例》要点: 本文介绍了PHP实战:基于Laravel5.4实现多字段登录功能方法示例,希望对您有用。如果有疑问,可以联系我们。 前言 PHP实例 最近在一个项目中需要实现一个多字段登录功能,简单来说就是可以使用

《PHP实战:基于Laravel5.4实现多字段登录功能方法示例》要点:
本文介绍了PHP实战:基于Laravel5.4实现多字段登录功能方法示例,希望对您有用。如果有疑问,可以联系我们。

前言PHP实例

最近在一个项目中需要实现一个多字段登录功能,简单来说就是可以使用用户名、邮箱或手机号任意一种方式进行登录.所以本文就来给大家介绍了关于Laravel5.4多字段登录的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧.PHP实例

以下内容基于laravel5.4PHP实例

方法如下:PHP实例

首先,通过artisan工具生成auth模块PHP实例

php artisan make:authPHP实例

这时候AppHttpControllers目录下会新增一个Auth目录,该目录下为注册登录相关的控制器,resourcesviews目录下也会生成一些与注册登录相关的视图PHP实例

laravel的官方文档中说手动认证用户需要使用IlluminateSupportFacadesAuth类的attempt方法,如下:PHP实例

<?php
namespace AppHttpControllers;
use IlluminateSupportFacadesAuth;
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');
  }
 }
}

这个方法会根据你传入的参数判断数据库中是否存在与之相匹配的用户,如果存在并且密码正确返回true,反之返回falsePHP实例

遂在LoginController中添加该方法,但是好像并没有效果PHP实例

于是开始观察LoginController的实现机制,发现它实现了一个AuthenticatesUsers的trait,追踪到这个trait的定义文件,发现这个文件就是我们想要的东西PHP实例

里面有一个login方法,就是负责处理登录的逻辑PHP实例

/**
  * Handle a login request to the application.
  *
  * @param IlluminateHttpRequest $request
  * @return IlluminateHttpRedirectResponse|IlluminateHttpResponse
  */
 public function login(Request $request)
 {
  // 表单验证
  $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.
  // 防止暴力破解,多次登录失败会根据IP锁定
  if ($this->hasTooManyLoginAttempts($request)) {
   $this->fireLockoutEvent($request);
   return $this->sendLockoutResponse($request);
  }
  
  // 这个就是主要的负责判断数据库中是否存在相应的账号和密码的地方,我们需要重写的就是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.
  // 登录失败,失败次数++,防止暴力破解
  $this->incrementLoginAttempts($request);
  // 返回失败响应
  return $this->sendFailedLoginResponse($request);
 }

分析了一波这个文件,发现主要进行登录判断的就是attemptLogin方法,我们只要重写这个方法即可,先看看原来的是怎么写的,根据原来的进行重写:PHP实例

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

在LoginController重写后:PHP实例

public function attemptLogin(Request $request)
 {
  $username = $request->input('username');
  $password = $request->input('password');
  // 验证用户名登录方式
  $usernameLogin = $this->guard()->attempt(
   ['username' => $username,'password' => $password],$request->has('remember')
  );
  if ($usernameLogin) {
   return true;
  }
  // 验证手机号登录方式
  $mobileLogin = $this->guard()->attempt(
   ['mobile' => $username,$request->has('remember')
  );
  if ($mobileLogin) {
   return true;
  }
  // 验证邮箱登录方式
  $emailLogin = $this->guard()->attempt(
   ['email' => $username,$request->has('remember')
  );
  if ($emailLogin) {
   return true;
  }
  return false;
 }

只需要用attempt方法进行多次判断即可,只要成功就返回true,不成功继续用其他字段进行判断,都不成功则返回flasePHP实例

测试,可以实现多字段登录效果PHP实例

总结PHP实例

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的支持.PHP实例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读