基于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设计模式 State (状态模式)
Jun 26 PHP
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
Dec 02 PHP
php实现aes加密类分享
Feb 16 PHP
php调用mysql存储过程实例分析
Dec 29 PHP
php技术实现加载字体并保存成图片
Jul 27 PHP
PHP实现清除wordpress里恶意代码
Oct 21 PHP
PHP闭包函数详解
Feb 13 PHP
php7安装mongoDB扩展的方法分析
Aug 02 PHP
php mysql数据库操作类(实例讲解)
Aug 06 PHP
php微信开发之音乐回复功能
Jun 14 PHP
PHP使用mongoclient简单操作mongodb数据库示例
Feb 08 PHP
laravel邮件发送的实现代码示例
Jan 31 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格式化工具Beautify PHP小小BUG
2008/04/24 PHP
php性能优化分析工具XDebug 大型网站调试工具
2011/05/22 PHP
[原创]PHP实现SQL语句格式化功能的方法
2017/07/28 PHP
PHP封装XML和JSON格式数据接口操作示例
2019/03/06 PHP
图片按比例缩放函数
2006/06/26 Javascript
JavaScript 学习点滴记录
2009/04/24 Javascript
jquery中ajax学习笔记一
2011/10/16 Javascript
Node.js中安全调用系统命令的方法(避免注入安全漏洞)
2014/12/05 Javascript
jQuery中size()方法用法实例
2014/12/27 Javascript
JavaScript修改浏览器tab标题小技巧
2015/01/06 Javascript
Backbone.js中的集合详解
2015/01/14 Javascript
jQuery实现信息提示框(带有圆角框与动画)效果
2015/08/07 Javascript
jquery日历插件datepicker用法分析
2016/01/22 Javascript
JS实现鼠标框选效果完整实例
2016/06/20 Javascript
JS实现的图片预览插件与用法示例【不上传图片】
2016/11/25 Javascript
详解如何从零开始搭建Express+Vue开发环境
2018/07/17 Javascript
vue2.0$nextTick监听数据渲染完成之后的回调函数方法
2018/09/11 Javascript
JavaScript实现像雪花一样的Hexaflake分形
2020/07/07 Javascript
微信小程序开发数据缓存基础知识辨析及运用实例详解
2020/11/06 Javascript
Python文件去除注释的方法
2015/05/25 Python
详解JavaScript编程中的window与window.screen对象
2015/10/26 Python
Python面向对象类的继承实例详解
2018/06/27 Python
Django框架使用内置方法实现登录功能详解
2019/06/12 Python
树莓派+摄像头实现对移动物体的检测
2019/06/22 Python
python+numpy按行求一个二维数组的最大值方法
2019/07/09 Python
python pandas利用fillna方法实现部分自动填充功能
2020/03/16 Python
时装界的“朋克之母”:Vivienne Westwood
2017/07/06 全球购物
机关门卫岗位职责
2013/12/30 职场文书
有关打架的检讨书
2014/01/25 职场文书
养成教育经验材料
2014/05/26 职场文书
公务员上班玩游戏检讨书
2014/09/17 职场文书
药品销售内勤岗位职责
2015/04/13 职场文书
幼儿园卫生保健制度
2015/08/05 职场文书
HTML5简单实现添加背景音乐的几种方法
2021/05/12 HTML / CSS
超详细教你怎么升级Mysql的版本
2021/05/19 MySQL
Apache Pulsar集群搭建部署详细过程
2022/02/12 Servers