zoukankan      html  css  js  c++  java
  • 使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

    http://laravelacademy.org/post/3910.html

    Laravel Captcha

    1、安装

    我们通过 Composer 安装 Captcha 扩展包:

    composer require mews/captcha

    注:Windows中使用该扩展包还需要安装 GD2 扩展(在php.ini中取消php_gd2.dll前面的注释)。

    2、配置

    使用Captcha服务提供者之前还需要在config/app.php中注册服务提供者:

    'providers' => [
        // ...
        MewsCaptchaCaptchaServiceProvider::class,
    ]

    同时注册下相应门面:

    'aliases' => [
        // ...
        'Captcha' => MewsCaptchaFacadesCaptcha::class,
    ]

    如果要使用自定义的配置,还可以发布配置文件到config目录:

    $ php artisan vendor:publish

    编辑新生成的captcha.php

    return [
        'default' => [
            'length' => 5,
            'width' => 120,
            'height' => 36,
            'quality' => 90,
        ],
        // ...
    ];

    3、使用示例

    // app/Http/routes.php
    
    Route::any('captcha-test', function()
    {
        if (Request::getMethod() == 'POST')
        {
            $rules = ['captcha' => 'required|captcha'];
            $validator = Validator::make(Input::all(), $rules);
            if ($validator->fails())
            {
                echo '<p style="color: #ff0000;">Incorrect!</p>';
            }
            else
            {
                echo '<p style="color: #00ff30;">Matched :)</p>';
            }
        }
    
        $form = '<form method="post" action="captcha-test">';
        $form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">';
        $form .= '<p>' . captcha_img() . '</p>';
        $form .= '<p><input type="text" name="captcha"></p>';
        $form .= '<p><button type="submit" name="check">Check</button></p>';
        $form .= '</form>';
        return $form;
    });
    

    显示效果如下:

    Laravel Captcha

    如果要返回原生图片,可以调用这个函数:

    captcha();

    或者

    Captcha::create();

    如果要返回URL:

    captcha_src();

    或者

    Captcha::src();
    

    如果要返回HTML:

    captcha_img();

    我们这个示例中使用的就是这个函数,或者调用Captcha门面上的方法:

    Captcha::img();

    要使用配置文件captcha.php中不同的配置项,可以这样调用:

    captcha_img('flat');
    Captcha::img('inverse');
  • 相关阅读:
    h5 百度获取地址
    Vue Cli项目使用PDF.js预览pdf无法访问到viewer.html
    Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead...
    VUE ES6转ES5,(ios 10.2版本,白屏,猜想es6语法原因)
    Vue+element-ui Upload 的http-request自定义上传文件
    vuejs中如何动态拼接生成字段名
    el-cascader 使用
    hdu 3555 数位dp入门
    codeforces Registration system
    hdu 1166 敌兵布阵
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9523502.html
Copyright © 2011-2022 走看看