DEV Community

Cover image for Add Google ReCaptcha To Laravel Project
Arman Rahman
Arman Rahman

Posted on

Add Google ReCaptcha To Laravel Project

Without using any package we can easily add Google Recaptcha to the laravel project

Image description

Step 1: Go to the Google reCAPTCHA website (https://www.google.com/recaptcha/admin/create) and log in with your Google account.

Image description

Step 2: Choose the reCAPTCHA v2.
Step 3: Select the “I’m not a robot” Checkbox.
Step 4: In the Domains section, enter your website domain(s) where the reCAPTCHA will be used. For development purposes, you can use “localhost” as one of the domains.
Step 5: Accept the reCAPTCHA Terms of Service, and click the “Submit” button.

Step 6: add this on .env and replace it with your site key and secret

RECAPTCHA_SITE_KEY=[your_site_key]
RECAPTCHA_SECRET_KEY=[your_secret_key]
RECAPTCHA_SITE=https://www.google.com/recaptcha/admin/
Enter fullscreen mode Exit fullscreen mode

Step 7: Add this link to your head tag

<script async src="https://www.google.com/recaptcha/api.js"></script>
Enter fullscreen mode Exit fullscreen mode

Step 8: Add this to your form

<!-- Google Recaptcha Widget-->
<div class="g-recaptcha mt-4" data-sitekey={{config('services.recaptcha.key')}}></div>
Enter fullscreen mode Exit fullscreen mode

Step 9: Add this to the config/services.php file

'recaptcha' => [
        'key' => env('RECAPTCHA_SITE_KEY'),
        'secret' => env('RECAPTCHA_SECRET_KEY'),
    ]
Enter fullscreen mode Exit fullscreen mode

Step 10: validate with this

$request->validate([
'g-recaptcha-response' => 'required'
], [], [
'g-recaptcha-response.required' => 'Captcha is required',
])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)