Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rama6312's avatar

Laravel Captcha

How to make a laravel captcha? I need step by step to make a laravel captcha.

0 likes
7 replies
Snapey's avatar

Implemented reCaptcha last night as a custom validator.

I took some ideas from Jeffery's video https://laracasts.com/lessons/prevent-spam-with-recaptcha but then used a service provider to create a custom validation;

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;


class RecaptchaValidator extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('recaptcha', function ($field, $token, $parameters, $validator) {

            // check the token that has been passed by a CURL call to the server

            $payload = [
                'secret'   => config('services.recaptcha.secret'),
                'remoteip' => app('request')->getClientIp(),
                'response' => $token,
            ];

            $ch = curl_init(config('services.recaptcha.url'));

            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 15);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

            $recaptchaReply = json_decode(curl_exec($ch));

            curl_close($ch);

            if(isset($recaptchaReply->success)) {
                return $recaptchaReply->success;
            }
            return false;
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

url, key and secret are all in .env and loaded in config/services.php

Code on my form as per the recaptcha documentation

Use the validator like;

    'g-recaptcha-response' => 'recaptcha|required'

Custom error messages in the language validation file;

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
        'g-recaptcha-response' => [
            'recaptcha' => 'reCAPTCHA does not think you are a human - only humans allowed.',
            'required' => 'Please check the box to confirm you are a real person.'
        ],
    ],

Doing it as a validator means I only need to add the recaptcha script and the form component, then add the validator. Only google approved requests make it through to my controller.

1 like
anam's avatar

You can try https://github.com/anam-hossain/captcha. This package is support both reCAPTCHA V2 and Invisible reCAPTCHA.

This package comes with a very neat api which is very easy to use. The package is only developed for Laravel users. In addition, Blade directive is used for importing reCaptcha front end code.

Example code:

Frond end

<form method="POST" action="/captcha" id="captcha-form">
  {{ csrf_field() }}
  <label>Name</label>
  <input type="text" name="name">
  <br>
  @captcha()
  <br>
  <input type="submit" value="Submit">
</form>

Server side

use Anam\Captcha\Captcha;
use Illuminate\Http\Request;

class CaptchaController extends Controller
{
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Anam\Captcha\Captcha  $captcha
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Captcha $captcha)
    {
        $response = $captcha->check($request);

        if (! $response->isVerified()) {
            dd($response->errors());
        }
        
        dd($response->hostname());
    }
}

Please or to participate in this conversation.