基于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中使用sockets:从新闻组中获取文章
Oct 09 PHP
用PHP4访问Oracle815
Oct 09 PHP
php Memcache 中实现消息队列
Nov 24 PHP
php算开始时间到过期时间的相隔的天数
Jan 12 PHP
php4与php5的区别小结(配置异同)
Dec 20 PHP
PHP如何利用P3P实现跨域
Aug 24 PHP
Yii操作数据库的3种方法
Mar 11 PHP
php中file_exists函数使用详解
May 08 PHP
PHP编写daemon process 实例详解
Nov 13 PHP
ZendFramework框架实现连接两个或多个数据库的方法
Dec 08 PHP
Yii2 批量插入、更新数据实例
Mar 15 PHP
[原创]PHP实现生成vcf vcard文件功能类定义与使用方法详解【附demo源码下载】
Sep 02 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
将数字格式的计算结果转为汉字格式
2006/10/09 PHP
PHP 杂谈《重构-改善既有代码的设计》之四 简化条件表达式
2012/04/09 PHP
Codeigniter操作数据库表的优化写法总结
2014/06/12 PHP
php检查字符串中是否包含7位GSM字符的方法
2015/03/17 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
2016/01/05 PHP
php生成验证码,缩略图及水印图的类分享
2016/04/07 PHP
PHP中call_user_func_array回调函数的用法示例
2016/11/26 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
2016/12/15 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
PhpStorm2020.1 安装 debug - Postman 调用的详细教程
2020/08/17 PHP
使用jQuery简化Ajax开发 Ajax开发入门
2009/10/14 Javascript
A标签中通过href和onclick传递的this对象实现思路
2013/04/19 Javascript
JS常见问题之为什么点击弹出的i总是最后一个
2016/01/05 Javascript
AngularJS基础 ng-init 指令简单示例
2016/08/02 Javascript
使用jQuery调用XML实现无刷新即时聊天
2016/08/07 Javascript
React-Native实现ListView组件之上拉刷新实例(iOS和Android通用)
2017/07/11 Javascript
使用jquery-easyui的布局layout写后台管理页面的代码详解
2019/06/19 jQuery
微信小程序背景音乐开发详解
2019/12/12 Javascript
[07:31]DOTA2卡尔工作室 英雄介绍主宰篇
2013/06/25 DOTA
[02:28]DOTA2亚洲邀请赛 LGD战队巡礼
2015/02/03 DOTA
如何利用Fabric自动化你的任务
2016/10/20 Python
带你了解python装饰器
2017/06/15 Python
python print输出延时,让其立刻输出的方法
2019/01/07 Python
安装python及pycharm的教程图解
2019/10/10 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
2020/04/08 Python
pytorch查看模型weight与grad方式
2020/06/24 Python
pycharm + django跨域无提示的解决方法
2020/12/06 Python
美国杂志订阅折扣与优惠网站:Magazines.com
2016/08/31 全球购物
《月光启蒙》教学反思
2014/03/01 职场文书
党员对照检查材料整改措施思想汇报
2014/09/26 职场文书
医院护士党的群众路线教育实践活动对照检查材料思想汇报
2014/10/04 职场文书
计划生育证明格式及范本
2014/10/09 职场文书
物业公司管理制度
2015/08/05 职场文书
朋友聚会祝酒词
2015/08/10 职场文书
HR必备:销售经理聘用合同范本
2019/08/21 职场文书
Oracle 11g数据库使用expdp每周进行数据备份并上传到备份服务器
2022/06/28 Oracle