CAPTCHA for Laravel 5 application Generation Verification code example

Source: Internet
Author: User
Tags button type pack

1, installation

We install the Captcha expansion pack via Composer:

Composer require Mews/captcha

Note: Using this expansion pack in Windows also requires the installation of the GD2 extension (php.ini to cancel Php_gd2.dll in the previous comment).

2, configuration

You also need to register your service provider in config/app.php before using the CAPTCHA service provider:

' Providers ' => [
// ...
Mews\captcha\captchaserviceprovider::class,
]

Register the corresponding façade at the same time:

' Aliases ' => [
// ...
' Captcha ' => mews\captcha\facades\captcha::class,
]

If you want to use a custom configuration, you can also publish the configuration file to the Config directory:

$ PHP Artisan Vendor:publish

To edit a newly generated captcha.php:

return [
' Default ' => [
' Length ' => 5,
' Width ' => 120,
' Height ' => 36,
' Quality ' => 90,
],
// ...
];

3, using the example

app/http/routes.php

Route::any (' Captcha-test ', function ()
{
    if (request::getmethod () = ' POST ')
 & nbsp;  {
        $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;
});


The display effect is as follows:

If you want to return to a native picture, you can call this function:

Captcha ();
Or

Captcha::create ();
If you want to return a URL:

CAPTCHA_SRC ();
Or

CAPTCHA::SRC ();
If you want to return HTML:

Captcha_img ();
This is the function that we use in this example, or we call the method on the captcha façade:

Captcha::img ();
To use different configuration items in the profile captcha.php, you can call this:

Captcha_img (' flat ');
Captcha::img (' inverse ');

Attention:

In Laravel 5.2, this package will show that the verification code does not verify the success of the problem, because 5.2 uses the middleware grouping, and does not use the global middleware cause this package does not enable session to cause the above problem. Solution: In

vendor/mews/captcha/src/captchaserviceprovider.php
29 Lines of

this->app[' router ']->get (' Captcha/{config?} ', ' \mews\captcha\captchacontroller@getcaptcha ')
Add later

->middleware (' web ');

Make sure that you have startsession middleware in your web middleware grouping. Alternatively you can add 5.2 new features to the frequency limit to the back

->middleware (' throttle:60,1 ')

To prevent malicious attacks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.