基于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实现图象锐化代码
Jun 14 PHP
php生成excel列序号代码实例
Dec 24 PHP
smarty获得当前url的方法分享
Feb 14 PHP
php使用cookie保存用户登录的用户名实例
Jan 26 PHP
php通过正则表达式记取数据来读取xml的方法
Mar 09 PHP
php实现插入数组但不影响原有顺序的方法
Mar 27 PHP
symfony2.4的twig中date用法分析
Mar 18 PHP
浅谈PHP中的
Apr 23 PHP
详谈PHP程序Laravel 5框架的优化技巧
Jul 18 PHP
PHP基于单例模式编写PDO类的方法
Sep 13 PHP
PHP实现生成带背景的图形验证码功能
Oct 03 PHP
PHP实现的折半查找算法示例
Dec 19 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
加强版phplib的DB类
2008/03/31 PHP
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
2013/06/24 PHP
使用PHP进行微信公众平台开发的示例
2015/08/21 PHP
php实现微信扫码支付
2017/03/26 PHP
把textarea中字符串里含有的回车换行替换成&amp;lt;br&amp;gt;的javascript代码
2007/04/20 Javascript
把JS与CSS写在同一个文件里的书写方法
2007/06/02 Javascript
基于jquery实现的鼠标拖拽元素复制并写入效果
2011/08/23 Javascript
{}与function(){}选用空对象{}来存放keyValue
2012/05/23 Javascript
如何让div span等元素能响应键盘事件操作指南
2012/11/13 Javascript
jquery实现的一个简单进度条效果实例
2014/05/12 Javascript
Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
2015/01/01 NodeJs
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
JS+CSS实现的简单折叠展开多级菜单效果
2015/09/12 Javascript
jQuery实现鼠标经过显示动画边框特效
2017/03/24 jQuery
JS实现的贪吃蛇游戏完整实例
2019/01/18 Javascript
electron实现静默打印的示例代码
2019/08/12 Javascript
ES5 模拟 ES6 的 Symbol 实现私有成员功能示例
2020/05/06 Javascript
javascript实现前端分页功能
2020/11/26 Javascript
[00:36]DOTA2上海特级锦标赛 Archon战队宣传片
2016/03/04 DOTA
[01:54]TI珍贵瞬间系列(三):翻盘
2020/08/28 DOTA
python获得两个数组交集、并集、差集的方法
2015/03/27 Python
Python抓取百度查询结果的方法
2015/07/08 Python
不可错过的十本Python好书
2017/07/06 Python
Python3 实现文件批量重命名示例代码
2019/06/03 Python
Python用Try语句捕获异常的实例方法
2019/06/26 Python
Python中*args和**kwargs的区别详解
2019/09/17 Python
python通过实例讲解反射机制
2019/10/17 Python
Python FtpLib模块应用操作详解
2019/12/12 Python
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
2020/02/11 Python
美国潜水装备、水肺潜水和浮潜设备商店:Leisure Pro
2018/08/08 全球购物
什么是lambda函数
2013/09/17 面试题
促销活动策划方案
2014/01/12 职场文书
大专生求职信
2014/06/29 职场文书
模具设计与制造专业自荐书
2014/07/01 职场文书
党的群众路线教育实践活动对照检查材料(四风)
2014/09/27 职场文书
vue3如何优雅的实现移动端登录注册模块
2021/03/29 Vue.js