Captcha validation is very important for protecting form submission from the spammer. It helps us by protecting specious form submission to our site by bots. There are many captcha solutions for reducing spam form submission out there but the Google reCaptcha is more secure, reliable and also it's free. If you are looking for a clear step by step tutorial on how to integrate Google reCaptcha with Laravel framework without using any extra package than it's for you. Here I'm going to show you a full step by step instruction to integrate Google reCaptcha with Laravel Framework completely from scratch without using any extra package installation.
By default, the Laravel ships a lot of validation rules but there is no rule for validating Google reCaptcha. We can easily make our own custom validation rule for validating Google reCaptcha. To do that run the command below for making a custom reCaptcha validation rules.
php artisan make:rule Recaptcha
with this command, a Recaptcha class will be created in the app/Rules directory. Now do the code in that class for validating Google reCaptcha. The cool part is we can easily reuse this rule in our next project.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Recaptcha implements Rule
{
public function __construct()
{
}
public function passes($attribute, $value)
{
$data = array(
'secret' => env('GOOGLE_RECAPTCHA_SECRET'),
'response' => $value
);
try {
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
return json_decode($response)->success;
} catch (\Exception $e) {
return false;
}
}
public function message()
{
return 'ReCaptcha verification failed.';
}
}
Collect site key and secret key from Google reCaptcha admin.
After adding your site, you will get your site key and secret key. Collect those keys and save in env file.
Put the site key and secret key into env file which you got in step no 2.
GOOGLE_RECAPTCHA_KEY=6LeIxAcTAABBAAJcZVRqyHhEQ#EGDW_MXjiZKhI
GOOGLE_RECAPTCHA_SECRET=6LeIxAD$@#AAAGG-vFI1TnRWxMZNFD#$jJifJWe
Now we have to add the Google reCaptcha script and component where we need protection from spam form submission. Here I have shown you for a contact form. You can use it in any place where you required to protect form submission from spammers.
<form method="POST" action="{{url('/contact')}}">
@csrf
<label>Name</label>
<input name="name">
<label>E-mail</label>
<input name="email">
<label>Message</label>
<input name="message">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" id="feedback-recaptcha" data-sitekey="{{ env('GOOGLE_RECAPTCHA_KEY') }}"></div>
</form>
We have done all pre-requisite steps, now the final step where we will validate our form request with Google reCaptcha like as normal Laravel validation by our custom Laravel validation rules which we have created at step no 1.
public function postContactForm(Request $request){
$this->validate($request, [
'g-recaptcha-response' => ['required', new Recaptcha()]
]);
// Recaptcha passed, do what ever you need
}
Hope this step by step tutorial for Google reCaptcha integration with Laravel from scratch helps you to add reCaptcha validation in your website or application. If this tutorial post help for you then please share it with others for helping them who are seeking for a tutorial on Laravel Google reCaptcha integration.