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


Posted in PHP onAugust 11, 2017

前言

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

以下内容基于laravel5.4

方法如下:

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

php artisan make:auth

这时候App\Http\Controllers目录下会新增一个Auth目录,该目录下为注册登录相关的控制器,resources\views目录下也会生成一些与注册登录相关的视图

laravel的官方文档中说手动认证用户需要使用Illuminate\Support\Facades\Auth类的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');
  }
 }
}

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

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

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

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

/**
  * Handle a login request to the application.
  *
  * @param \Illuminate\Http\Request $request
  * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
  */
 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方法,我们只要重写这个方法即可,先看看原来的是怎么写的,根据原来的进行重写:

/**
  * 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');

  // 验证用户名登录方式
  $usernameLogin = $this->guard()->attempt(
   ['username' => $username, 'password' => $password], $request->has('remember')
  );
  if ($usernameLogin) {
   return true;
  }

  // 验证手机号登录方式
  $mobileLogin = $this->guard()->attempt(
   ['mobile' => $username, 'password' => $password], $request->has('remember')
  );
  if ($mobileLogin) {
   return true;
  }

  // 验证邮箱登录方式
  $emailLogin = $this->guard()->attempt(
   ['email' => $username, 'password' => $password], $request->has('remember')
  );
  if ($emailLogin) {
   return true;
  }

  return false;
 }

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

PHP 相关文章推荐
php中的登陆login
Jan 18 PHP
php批量缩放图片的代码[ini参数控制]
Feb 11 PHP
php插入中文到sqlserver 2008里出现乱码的解决办法分享
Jul 19 PHP
PHP set_error_handler()函数使用详解(示例)
Nov 12 PHP
windows7下php开发环境搭建图文教程
Jan 06 PHP
PHP记录和读取JSON格式日志文件
Jul 07 PHP
PHP 验证身份证是否合法的函数
Feb 09 PHP
php生成毫秒时间戳的实例讲解
Sep 22 PHP
微信公众平台开发教程⑥ 微信开发集成类的使用图文详解
Apr 10 PHP
thinkPHP5.1框架中Request类四种调用方式示例
Aug 03 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
Oct 28 PHP
阿里云服务器搭建Php+Apache运行环境的详细过程
May 15 PHP
PHP递归实现文件夹的复制、删除、查看大小操作示例
Aug 11 #PHP
关于PHP中协程和阻塞的一些理解与思考
Aug 11 #PHP
如何利用预加载优化Laravel Model查询详解
Aug 11 #PHP
PHP实现的自定义图像居中裁剪函数示例【测试可用】
Aug 11 #PHP
Redis在Laravel项目中的应用实例详解
Aug 11 #PHP
PHP验证码无法显示的原因及解决办法
Aug 11 #PHP
php readfile()修改文件上传大小设置
Aug 11 #PHP
You might like
php2html php生成静态页函数
2008/12/08 PHP
Zend Framework框架路由机制代码分析
2016/03/22 PHP
最简单的jQuery程序 入门者学习
2009/07/09 Javascript
基于jquery ajax 用户无刷新登录方法详解
2012/04/28 Javascript
JavaScript String.replace函数参数实例说明
2013/06/06 Javascript
js 绑定键盘鼠标事件示例代码
2014/02/12 Javascript
Javascript学习笔记之函数篇(六) : 作用域与命名空间
2014/11/23 Javascript
js实现选中页面文字将其分享到新浪微博
2015/11/05 Javascript
浅析JS动态创建元素【两种方法】
2016/04/20 Javascript
ES6记录异步函数的执行时间详解
2016/08/31 Javascript
微信小程序 scroll-view组件实现列表页实例代码
2016/12/14 Javascript
jQuery、zepto、js常用小技巧
2017/02/12 Javascript
seaJs使用心得之exports与module.exports的区别实例分析
2017/10/13 Javascript
vue2中使用sass并配置全局的sass样式变量的方法
2018/09/04 Javascript
react-router 路由切换动画的实现示例
2018/12/03 Javascript
35个最好用的Vue开源库(史上最全)
2019/01/03 Javascript
微信小程序云开发 生成带参小程序码流程
2019/05/18 Javascript
python进阶教程之词典、字典、dict
2014/08/29 Python
python+selenium 定位到元素,无法点击的解决方法
2019/01/30 Python
Python中按值来获取指定的键
2019/03/04 Python
详解Matplotlib绘图之属性设置
2019/08/23 Python
TensorFlow 输出checkpoint 中的变量名与变量值方式
2020/02/11 Python
pytorch 常用函数 max ,eq说明
2020/06/28 Python
Pycharm2020.1安装无法启动问题即设置中文插件的方法
2020/08/07 Python
M1芯片安装python3.9.1的实现
2021/02/02 Python
英格兰橄榄球商店:England Rugby Store
2016/12/17 全球购物
巴西在线鞋店:Shoestock
2017/10/28 全球购物
Chemist Warehouse中文网:澳洲连锁大药房
2021/02/05 全球购物
Laravel中Kafka的使用详解
2021/03/24 PHP
编辑硕士自荐信范文
2013/11/27 职场文书
办公室内勤岗位职责范本
2013/12/09 职场文书
文案策划求职信
2014/04/14 职场文书
会议接待欢迎词范文
2015/01/26 职场文书
施工安全员岗位职责
2015/04/11 职场文书
工作调动申请报告
2015/05/18 职场文书
企业法人任命书
2015/09/21 职场文书