基于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 相关文章推荐
PHP4实际应用经验篇(6)
Oct 09 PHP
php2html php生成静态页函数
Dec 08 PHP
ThinkPHP3.0略缩图不能保存到子目录的解决方法
Sep 30 PHP
基于PHP常用函数的用法详解
May 10 PHP
php三维数组去重(示例代码)
Nov 26 PHP
以文件形式缓存php变量的方法
Jun 26 PHP
Zend Framework教程之Application和Bootstrap用法详解
Mar 10 PHP
thinkphp3.2中实现phpexcel导出带生成图片示例
Feb 14 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
Oct 17 PHP
Laravel 在views中加载公共页面的实现代码
Oct 22 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
Feb 27 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
Apr 06 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
header中Content-Disposition的作用与使用方法
2012/06/13 PHP
php自动加载机制的深入分析
2013/06/08 PHP
基于empty函数的输出详解
2013/06/17 PHP
PHP 面向对象程序设计(oop)学习笔记 (五) - PHP 命名空间
2014/06/12 PHP
PHP7数组的底层实现示例
2019/08/25 PHP
实现laravel 插入操作日志到数据库的方法
2019/10/11 PHP
JSQL  一个 web DB 的封装
2010/05/05 Javascript
window.event.keyCode兼容IE和Firefox实现js代码
2013/05/30 Javascript
两种方法解决javascript url post 特殊字符转义 + &amp; #
2016/04/13 Javascript
微信小程序 WXDropDownMenu组件详解及实例代码
2016/10/24 Javascript
jquery popupDialog 使用 加载jsp页面的方法
2016/10/25 Javascript
Bootstrap笔记—折叠实例代码
2017/03/13 Javascript
如何选择jQuery版本 1.x? 2.x? 3.x?
2017/04/01 jQuery
史上最全JavaScript数组去重的十种方法(推荐)
2017/08/17 Javascript
利用jquery如何从json中读取数据追加到html中
2017/12/01 jQuery
Vue.js组件间的循环引用方法示例
2017/12/27 Javascript
微信小程序(订阅消息)功能
2019/10/25 Javascript
javascript操作元素的常见方法小结
2019/11/13 Javascript
微信小程序分享小程序码的生成(带参数)以及参数的获取
2020/03/25 Javascript
vue中使用带隐藏文本信息的图片、图片水印的方法
2020/04/24 Javascript
微信小程序实现watch监听
2020/06/04 Javascript
jQuery实现可以计算进制转换的计算器
2020/10/19 jQuery
vue render函数动态加载img的src路径操作
2020/10/26 Javascript
[01:11:10]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG加赛
2014/05/26 DOTA
详解python调度框架APScheduler使用
2017/03/28 Python
django manage.py扩展自定义命令方法
2018/05/27 Python
python提取具有某种特定字符串的行数据方法
2018/12/11 Python
python Django 创建应用过程图示详解
2019/07/29 Python
解决Django layui {{}}冲突的问题
2019/08/29 Python
python爬虫基础知识点整理
2020/06/02 Python
理肤泉加拿大官网:La Roche-Posay加拿大
2018/07/06 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
法学研究生自我鉴定范文
2013/12/04 职场文书
集体备课反思
2014/02/12 职场文书
幼儿园见习总结
2015/06/23 职场文书
详解Spring Bean的配置方式与实例化
2022/06/10 Java/Android