Blogger Information
Blog 60
fans 5
comment 3
visits 65080
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Laravel实现后台登录页面
longlong
Original
1024 people have browsed it

一、Laravel实现后台登录页面

  • Account.php 账户控制器
  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. // 账户相关控制器
  7. class Account extends Controller
  8. {
  9. // 控制登录页面
  10. public function login(){
  11. return view('/admins/account/login');
  12. }
  13. // 生成验证码图片
  14. public function get_captcha(){
  15. VeriCode::create();
  16. }
  17. // 执行登录操作
  18. public function doLogin(Request $request){
  19. // 获取账户信息以及验证码
  20. $username = trim($request->username);
  21. $pwd = trim($request->password);
  22. $vericode = trim($request->vericode);
  23. session_start();
  24. $code = $_SESSION['code'];
  25. // 验证账户信息
  26. if($vericode == '')
  27. exit(json_encode(['code'=>401,'msg'=>'请输入验证码']));
  28. if(strtolower($vericode) != strtolower($code))
  29. exit(json_encode(['code'=>401,'msg'=>'验证码错误']));
  30. if($username=='' || $pwd=='')
  31. exit(json_encode(['code'=>401,'msg'=>'用户名或密码不能为空']));
  32. if(!Auth::attempt(['username'=>$username,'password'=>$pwd]))
  33. exit(json_encode(['code'=>401,'msg'=>'用户名或密码错误']));
  34. dump($username);
  35. dump($pwd);
  36. dump($vericode);
  37. }
  38. }
  39. /**
  40. * 验证码类
  41. */
  42. class VeriCode{
  43. // 获取验证码配置
  44. private static function _getCodeConfig(){
  45. return [
  46. // 验证码字符集
  47. 'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  48. // 验证码个数
  49. 'codeCount' => 4,
  50. // 字体大小
  51. 'fontsize' =>16,
  52. // 验证码的宽度
  53. 'width' => 100,
  54. // 验证码高度
  55. 'height' => 36,
  56. // 是否有干扰点?true有,false没有
  57. 'disturbPoint' => true,
  58. // 干扰点个数,disturbPoint开启后生效
  59. 'pointCount' => 200,
  60. // 是否有干扰条?true有,false没有
  61. 'disturbLine' => true,
  62. // 干扰条个数,disturbLine开启后生效
  63. 'lineCount' => 3
  64. ];
  65. }
  66. // 创建图片验证码
  67. public static function create(){
  68. // 配置
  69. $config = self::_getCodeConfig();
  70. //创建画布
  71. $image = imagecreatetruecolor($config['width'],$config['height']);
  72. //背景颜色
  73. $bgcolor=imagecolorallocate($image,255,255,255);
  74. imagefill($image,0,0,$bgcolor);
  75. $captch_code = '';//存储验证码
  76. $captchCodeArr = str_split($config['codeStr']);
  77. //随机选取4个候选字符
  78. for($i=0;$i<$config['codeCount'];$i++){
  79. $fontsize = $config['fontsize'];
  80. $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
  81. $fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];
  82. $captch_code.=$fontcontent;
  83. $_x = $config['width']/$config['codeCount'];
  84. $x=($i*(int)$_x)+rand(5,10); //随机坐标
  85. $y=rand(5,10);
  86. imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串
  87. }
  88. session_start();
  89. $_SESSION['code']=$captch_code;
  90. //增加干扰点
  91. if($config['disturbPoint']){
  92. for($i=0;$i<$config['pointCount'];$i++){
  93. $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
  94. imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
  95. }
  96. }
  97. //增加干扰线
  98. if($config['disturbLine']){
  99. for($i=0;$i<$config['lineCount'];$i++){
  100. $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
  101. imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
  102. }
  103. }
  104. //输出格式
  105. header('content-type:image/png');
  106. imagepng($image);
  107. //销毁图片
  108. imagedestroy($image);
  109. }
  110. }
  • User.php中增加项:
  1. class User extends Authenticatable
  2. {
  3. protected $table = 'admin';
  4. }
  • Web.php 路由增加项:
  1. Route::get('/admins/account/login','admins\Account@login');
  2. Route::get('/admins/account/get_captcha','admins\Account@get_captcha');
  3. Route::post('/admins/account/doLogin','admins\Account@doLogin');
  • login.blade.php 登录页面
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>后台登录系统</title>
  9. <link rel="stylesheet" href="/static/layui/css/layui.css">
  10. <link rel="stylesheet" href="/static/css/admins.css">
  11. <script src="/static/layui/layui.js"></script>
  12. </head>
  13. <body>
  14. <form class="layui-form" action="" >
  15. @csrf
  16. <p>后台登录系统</p>
  17. <hr>
  18. <div class="layui-form-item">
  19. <label class="layui-form-label">用户名</label>
  20. <div class="layui-input-block">
  21. <input type="text" id="username" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
  22. </div>
  23. </div>
  24. <div class="layui-form-item">
  25. <label class="layui-form-label">密码</label>
  26. <div class="layui-input-block">
  27. <input type="password" id="pwd" name="pwd" placeholder="请输入密码" autocomplete="off" class="layui-input">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <label class="layui-form-label">验证码</label>
  32. <div class="layui-input-inline">
  33. <input type="text" id="vericode" name="vericode" placeholder="请输入验证码" autocomplete="off" class="layui-input">
  34. </div>
  35. <img src="/admins/account/get_captcha" alt="看不清,点击刷新" style="cursor: pointer">
  36. </div>
  37. <div class="layui-form-item">
  38. <div class="layui-input-block">
  39. <button type="submit" class="layui-btn btn-login">登录</button>
  40. <button type="reset" class="layui-btn layui-btn-primary">重置</button>
  41. </div>
  42. </div>
  43. </form>
  44. <script>
  45. // 获取layui弹出层组件
  46. layui.use('layer',function () {
  47. layer = layui.layer;
  48. $ = layui.jquery;
  49. // layer.msg('hello');
  50. })
  51. // 验证码实时刷新
  52. const img = document.querySelector('img');
  53. img.addEventListener('click',function () {
  54. img.src = '/admins/account/get_captcha?'+Math.random();
  55. });
  56. // 执行登录操作
  57. document.querySelector('.btn-login').addEventListener('click',function (ev) {
  58. // 禁用表单默认提交行为
  59. ev.preventDefault();
  60. // console.log('111');
  61. // 获取用户输入的信息
  62. let username = $('#username').val();
  63. let pwd = $('#pwd').val();
  64. let vericode = $('#vericode').val();
  65. let _token = $('input[name="_token"]').val();
  66. // 提交信息去验证
  67. $.post('/admins/account/doLogin',{username:username,password:pwd,vericode:vericode,_token:_token},function (data) {},'json');
  68. })
  69. </script>
  70. </body>
  71. </html>
  • 例:数据表如下:

登录效果如下:

  • 验证码输入错误时:

  • 验证码正确,用户信息错误时:

  • 所有信息都正确时:

二、总结登录流程

  1. 先创建账户控制器,然后定义一个方法用于跳转到登录页面,切记要写路由

  2. 登录页面样式写好以后,设置点击登录按钮时将信息提交处理(还有一个重点部分在于表单中的验证码如何引入)

  3. 在控制器中又创建方法用于接收账户信息,进行相应的处理和过滤

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!