基于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_xmlhttp 乱码问题解决方法
Aug 07 PHP
在Windows系统上安装PHP运行环境文字教程
Jul 19 PHP
在WINDOWS中设置计划任务执行PHP文件的方法
Dec 19 PHP
str_replace只替换一次字符串的方法
Apr 09 PHP
探讨方法的重写(覆载)详解
Jun 08 PHP
PHP的PSR规范中文版
Sep 28 PHP
PHP扩展模块Pecl、Pear以及Perl的区别
Apr 09 PHP
ThinkPHP模板Switch标签用法示例
Jun 30 PHP
Chrome Web App开发小结
Sep 04 PHP
smarty缓存用法分析
Dec 16 PHP
ecshop 2.72如何修改后台访问地址
Mar 03 PHP
php约瑟夫问题解决关于处死犯人的算法
Mar 23 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
生成卡号php代码
2008/04/09 PHP
yii2实现根据时间搜索的方法
2016/05/25 PHP
Codeigniter里的无刷新上传的实现代码
2019/04/14 PHP
图片自动更新(说明)
2006/10/02 Javascript
用javascript父窗口控制只弹出一个子窗口
2007/04/10 Javascript
JavaScript 异步调用框架 (Part 4 - 链式调用)
2009/08/04 Javascript
JavaScript高级程序设计(第3版)学习笔记2 js基础语法
2012/10/11 Javascript
js 三级关联菜单效果实例
2013/08/13 Javascript
js toFixed()方法的重写实现精度的统一
2014/03/06 Javascript
setTimeout内不支持jquery的选择器的解决方案
2015/04/28 Javascript
关注jquery技巧提高jquery技能(前端开发必学)
2015/11/02 Javascript
JS实现图片平面旋转的方法
2016/03/01 Javascript
easyui validatebox验证
2016/04/29 Javascript
浅谈jquery中使用canvas的问题
2016/10/10 Javascript
5种JavaScript脚本加载的方式
2017/01/16 Javascript
Angular.js中数组操作的方法教程
2017/07/31 Javascript
Vuex mutitons和actions初使用详解
2019/03/04 Javascript
vue组件实现移动端九宫格转盘抽奖
2020/10/16 Javascript
python爬虫入门教程--利用requests构建知乎API(三)
2017/05/25 Python
Python中列表list以及list与数组array的相互转换实现方法
2017/09/22 Python
Python通过OpenCV的findContours获取轮廓并切割实例
2018/01/05 Python
Python 数据处理库 pandas进阶教程
2018/04/21 Python
python爬虫简单的添加代理进行访问的实现代码
2019/04/04 Python
对numpy下的轴交换transpose和swapaxes的示例解读
2019/06/26 Python
使用 python pyautogui实现鼠标键盘控制功能
2019/08/04 Python
Python常用库Numpy进行矩阵运算详解
2020/07/21 Python
降低python版本的操作方法
2020/09/11 Python
html5如何在Canvas中实现自定义路径动画示例
2017/09/18 HTML / CSS
体验完美剃须:The Art of Shaving
2018/08/06 全球购物
都柏林通行卡/城市通票:The Dublin Pass
2020/02/16 全球购物
技能比赛获奖感言
2014/02/14 职场文书
事业单位鉴定材料
2014/05/25 职场文书
县人大领导班子四风对照检查材料思想汇报
2014/10/09 职场文书
谢师宴学生致辞
2015/07/27 职场文书
Java实现多线程聊天室
2021/06/26 Java/Android
利用For循环遍历Python字典的三种方法实例
2022/03/25 Python