Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Securimage captcha with Laravel 5.3

Tags:

laravel-5.3

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;
});
like image 341
Toni Avatar asked Feb 06 '17 14:02

Toni


1 Answers

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']);

Here i will explain, why

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']);
    });
like image 159
shock_gone_wild Avatar answered Sep 21 '22 17:09

shock_gone_wild