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.