laravel 使用验证码

laravel 拥有composer这个包管理工具 使用相关依赖就变得容易的多 

使用验证码,首先php要开启gd库

 

这次我们使用扩展库是gregwar/captcha

 

1.在根目录coposer.json添加如下

"require": {  
        ...  
        "gregwar/captcha": "1.*"  
    },  

2.使用composer 更新

composer update

3.调用库中的方法

<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class BackController extends Controller
{
  /**
     * 验证码生成
     * @param  [type] $tmp [description]
     * @return [type]      [description]
     */
    public function captcha($tmp)
    {
        //生成验证码图片的Builder对象,配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }  
}

4.注册路由

//生成验证码
Route::get('login/captcha/{tmp}', 'BackController@captcha');

5.view中使用

<img  src="{{ url('login/captcha/1') }}"  alt="验证码" title="刷新图片" width="100" height="40" id="captcha_img" border="0">

6.后台验证

      $code = $request->input('code');


      if(Session::get('milkcaptcha')!=$code) {
        return redirect('/')->with('message','验证码错误');
      }

  

 

转载于:https://www.cnblogs.com/zhoupufelix/p/7794129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值