基于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 相关文章推荐
我的论坛源代码(四)
Oct 09 PHP
cmd下运行php脚本
Nov 25 PHP
php调用mysql数据 dbclass类
May 07 PHP
PHP常用技巧总结(附函数代码)
Feb 04 PHP
PHP中将ip地址转成十进制数的两种实用方法
Aug 15 PHP
php中curl和file_get_content的区别
May 10 PHP
Laravel 4 初级教程之Pages、表单验证
Oct 30 PHP
PHP执行SQL文件并将SQL文件导入到数据库
Sep 17 PHP
php中关于长度计算容易混淆的问题分析
May 27 PHP
zen cart实现订单中增加paypal中预留电话的方法
Jul 12 PHP
PHP多进程编程实例详解
Jul 19 PHP
PHP实现支持CURL字符串证书传输的方法
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 Cookie处理函数
2016/06/10 PHP
PHP使用php-resque库配合Redis实现MQ消息队列的教程
2016/06/29 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
2017/04/27 PHP
在laravel中使用Symfony的Crawler组件分析HTML
2017/06/19 PHP
JavaScript 私有成员分析
2009/01/13 Javascript
JS运行耗时操作的延时显示方法
2010/11/19 Javascript
jQuery表格插件ParamQuery简单使用方法示例
2013/12/05 Javascript
后台获取ZTREE选中节点的方法
2015/02/12 Javascript
Javascript基础_嵌入图像的简单实现
2016/06/14 Javascript
JS实现环形进度条(从0到100%)效果
2016/07/05 Javascript
Vuex2.0+Vue2.0构建备忘录应用实践
2016/11/30 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
js防刷新的倒计时代码 js倒计时代码
2017/09/06 Javascript
JS数组方法push()、pop()用法实例分析
2020/01/18 Javascript
vue项目里面引用svg文件并给svg里面的元素赋值
2020/08/17 Javascript
[02:05]2014DOTA2西雅图邀请赛 老队长全明星大猜想谁不服就按进显示器
2014/07/08 DOTA
[14:56]教你分分钟做大人:巫医
2014/10/30 DOTA
使用python实现递归版汉诺塔示例(汉诺塔递归算法)
2014/04/08 Python
详细介绍Python函数中的默认参数
2015/03/30 Python
对Python新手编程过程中如何规避一些常见问题的建议
2015/04/01 Python
Python实现程序的单一实例用法分析
2015/06/03 Python
Python实现破解12306图片验证码的方法分析
2017/12/29 Python
使用Python监视指定目录下文件变更的方法
2018/10/15 Python
解决python3 pika之连接断开的问题
2018/12/18 Python
Django页面数据的缓存与使用的具体方法
2019/04/23 Python
Python Django 简单分页的实现代码解析
2019/08/21 Python
Python PyQt5运行程序把输出信息展示到GUI图形界面上
2020/04/27 Python
中软国际Java程序员机试题
2012/08/19 面试题
自我鉴定200字
2013/10/28 职场文书
医药学专业大学生职业生涯规划书论文
2014/01/21 职场文书
演讲稿的格式及范文
2014/08/22 职场文书
2015年行政工作总结范文
2015/04/09 职场文书
农村结婚典礼主持词
2015/06/29 职场文书
2019最新激励员工口号大全!
2019/06/28 职场文书
Python selenium的这三种等待方式一定要会!
2021/06/10 Python
Spring Security使用单点登录的权限功能
2022/04/03 Java/Android