Blogger Information
Blog 37
fans 0
comment 0
visits 20796
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
综合实战第六课:xpcms原理介绍-PHP培训九期线上班
渡劫小能手
Original
1060 people have browsed it

表设计原则

要考虑到以后单表体积

要把主要大字段分割出去

使用搜索引擎

记录多的话,要用第三方搜索引擎,分词器,elastic seacrch

后台登录

指定controller路径

用artisan创建控制器, php artisan make:controller admins/Account

<a name="KoKUE"></a>

创建路由

添加路由 Route::get('/admins/account/login','admins\Account@login'); ,注意命名空间反斜杠

  1. Route::get('/admins/account/login','admins\Account@login');

指定view路径

  1. return view('admins.account.login');

验证码

注意给验证码写路由

  1. /**
  2. * 验证码类
  3. */
  4. class VeriCode{
  5. // 获取验证码配置
  6. private static function _getCodeConfig(){
  7. return [
  8. // 验证码字符集
  9. 'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  10. // 验证码个数
  11. 'codeCount' => 4,
  12. // 字体大小
  13. 'fontsize' =>16,
  14. // 验证码的宽度
  15. 'width' => 100,
  16. // 验证码高度
  17. 'height' => 36,
  18. // 是否有干扰点?true有,false没有
  19. 'disturbPoint' => true,
  20. // 干扰点个数,disturbPoint开启后生效
  21. 'pointCount' => 200,
  22. // 是否有干扰条?true有,false没有
  23. 'disturbLine' => true,
  24. // 干扰条个数,disturbLine开启后生效
  25. 'lineCount' => 3
  26. ];
  27. }
  28. // 创建图片验证码
  29. public static function create(){
  30. // 配置
  31. $config = self::_getCodeConfig();
  32. //创建画布
  33. $image = imagecreatetruecolor($config['width'],$config['height']);
  34. //背景颜色
  35. $bgcolor=imagecolorallocate($image,255,255,255);
  36. imagefill($image,0,0,$bgcolor);
  37. $captch_code = '';//存储验证码
  38. $captchCodeArr = str_split($config['codeStr']);
  39. //随机选取4个候选字符
  40. for($i=0;$i<$config['codeCount'];$i++){
  41. $fontsize = $config['fontsize'];
  42. $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
  43. $fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];
  44. $captch_code.=$fontcontent;
  45. $_x = $config['width']/$config['codeCount'];
  46. $x=($i*(int)$_x)+rand(5,10); //随机坐标
  47. $y=rand(5,10);
  48. imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串
  49. }
  50. session_start();
  51. $_SESSION['code']=$captch_code;
  52. //增加干扰点
  53. if($config['disturbPoint']){
  54. for($i=0;$i<$config['pointCount'];$i++){
  55. $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
  56. imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
  57. }
  58. }
  59. //增加干扰线
  60. if($config['disturbLine']){
  61. for($i=0;$i<$config['lineCount'];$i++){
  62. $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
  63. imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
  64. }
  65. }
  66. //输出格式
  67. header('content-type:image/png');
  68. imagepng($image);
  69. //销毁图片
  70. imagedestroy($image);
  71. }
  72. }

登录提交

定义post提交路由,用ajax的post提交登录数据
使用post提交注意添加@csrf

  1. function dologin(){
  2. var username = $.trim($('#username').val());
  3. var pwd = $.trim($('#password').val());
  4. var verifycode = $.trim($('#verifycode').val());
  5. if(username==''){
  6. layer.alert('请输入用户名',{icon:2});
  7. return;
  8. }
  9. if(pwd==''){
  10. layer.alert('请输入密码',{icon:2});
  11. return;
  12. }
  13. if(verifycode==''){
  14. layer.alert('请输入验证码',{icon:2});
  15. return;
  16. }
  17. var _token = $('input[name="_token"]').val();
  18. $.post('/admins/account/dologin',{username:username,pwd:pwd,verifycode:verifycode,_token:_token},function(res){
  19. if(res.code>0){
  20. reload_captcha();
  21. return layer.alert(res.msg,{icon:2});
  22. }
  23. layer.alert(res.msg,{icon:1});
  24. setTimeout(function(){
  25. window.location.href='/admins/home/index';
  26. },1000);
  27. },'json');
  28. }

控制器中用Request接收

  1. public function dologin(Request $req){
  2. $username = trim($req->username);
  3. $pwd = trim($req->pwd);
  4. $VeriCode = trim($req->verifycode);
  5. if ($username==''){
  6. return json_encode(array('code'=>1,'msg'=>'用户不能为空'));
  7. }
  8. if ($pwd==''){
  9. return json_encode(array('code'=>1,'msg'=>'密码不能为空'));
  10. }
  11. session_start();
  12. if (strtolower($VeriCode)!=strtolower($_SESSION['code'])){
  13. return json_encode(array('code'=>1,'msg'=>'验证码不正确'));
  14. }
  15. return json_encode(array('code'=>0,'msg'=>'登录成功'));
  16. }
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!