I installed a laravel package for securimage captcha, everything seems to be fine(as in it show the random captcha), but even after putting the correct text, it still says invalid. can someone help me please. Here is my code
Route::any('/test-captcha', 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="test-captcha">';
$form .= '<input type="hidden" name="_token" value="' . csrf_token() . '">';
$form .= '<p>' . captcha_img() . '</p>';
$form .= '<p><button type="submit" name="check">Check</button></p>';
$form .= '</form>';
return $form;
});
Short resolution: Add this lines to your routes.php file:
Route::get('securimage', ['as' => 'securimage', 'uses' => '\Yhbyun\Securimage\SecurimageController@getCaptcha']);
Route::get('securimage/audio', ['as' => 'securimage.audio', 'uses' => '\Yhbyun\Securimage\SecurimageController@getAudio']);
Route::get('securimage/check', ['as' => 'securimage.check', 'uses' => '\Yhbyun\Securimage\SecurimageController@check']);
The Problem in this case is, that the package defines its routes in /vendor/yhbyun/laravel-securimage/src/SecurimageServiceProvider.php like this:
$app['router']->get('securimage', ['as' => 'securimage', 'uses' => 'Yhbyun\Securimage\SecurimageController@getCaptcha']);
$app['router']->get('securimage/audio', ['as' => 'securimage.audio', 'uses' => 'Yhbyun\Securimage\SecurimageController@getAudio']);
$app['router']->get('securimage/check', ['as' => 'securimage.check', 'uses' => 'Yhbyun\Securimage\SecurimageController@check']);
If you run php artisan route:list
you can see that there is no middleware applied, since In Laravel's Kernel.php file only the routes defined in routes.php are grouped with the 'web' middleware applied :
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web.php');
});
The web middleware group contains the Middleware for Starting Sessions:
'web' => [
// ....
\Illuminate\Session\Middleware\StartSession::class,
// .....
],
Maybe someone wants to create a pull request for the Laravel Versions that already use the 'web' middleware. This could be something like (inside SecurimageServiceProvider.php) :
$app['router']->group(['middleware' => 'web'], function() use ($app) {
$app['router']->get('securimage', ['as' => 'securimage', 'uses' => 'Yhbyun\Securimage\SecurimageController@getCaptcha']);
$app['router']->get('securimage/audio', ['as' => 'securimage.audio', 'uses' => 'Yhbyun\Securimage\SecurimageController@getAudio']);
$app['router']->get('securimage/check', ['as' => 'securimage.check', 'uses' => 'Yhbyun\Securimage\SecurimageController@check']);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With