![]() |
VOOZH | about |
Captcha validation is extremely vital for safeguarding kind submission from the sender. It helps us to protect specious kind submissions to our website by bots. There are several captcha solutions for reducing spam kind submission. Out of these, the Google reCaptcha is safer, reliable, and most important is it's free. If you are looking for how to add Google reCaptcha in your website then this article is useful for you. In this article, we learn about, using laravel framework how to add Google reCaptcha without adding external package installation.
Steps to laravel Google reCaptcha validation: Below is the few steps following which we can use Google reCaptcha without package:
1. Create a custom validation rule: By default, the laravel has many more rules of validation, but there is no rule for Google reCaptcha validation. But using one command we can make a simple custom validation rule for Google reCaptcha. By using the below command we make our custom validation rule
php artisan make:rule Recaptcha2. Create Recaptcha class: After successful execution of this command, the reCaptcha class will be created in the path app/Rules directory. Then write the below code in the reCaptcha class file. The important thing is this code can be reused in your next project.
3. Generate site key and secret key: Click on Google recapture you will get one page-like form, as shown in the picture below:
By adding your site and details you will get your website key and secret key. Save that key in your device notepad for further process.
4. Update .env file: Update the .env file of your project with a secret key and site key (Note: add your own site key and secret key, that is saved in your notepad).
GOOGLE_RECAPTCHA_KEY=<site key>
GOOGLE_RECAPTCHA_SECRET=<secret key>
5. Create a view file: Create a view or blade file to write the code where you want Google reCaptcha validation protection. You can use this code in any form that you want safely submit.
6. Google reCaptcha validation: In the final step, we add a custom validation rule at path app\Rules\GoogleRecaptcha. Add the below code to your file.
Output: