Download the PHP package alexstack/google-recaptcha-to-any-form without Composer
On this page you can find all versions of the php package alexstack/google-recaptcha-to-any-form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download alexstack/google-recaptcha-to-any-form
More information about alexstack/google-recaptcha-to-any-form
Files in alexstack/google-recaptcha-to-any-form
Package google-recaptcha-to-any-form
Short Description Display a Google Recaptcha v2 or v3 or invisible in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x & Laravel & Wordpress & other CMS.
License MIT
Homepage https://github.com/AlexStack/Google-Recaptcha-to-any-form
Informations about the package google-recaptcha-to-any-form
Google Recaptcha v2 or v3 to any form
- It can display a Google Recaptcha v2 or v3 or invisible in any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x & Larave & Wordpress & other CMS.
Basic example codes
-
Display Google Recaptcha v2 or v3 after a Form_Field_ID:
- Verify it in the backend php:
How to install
Contents
- [x] Invisible
- [x] How to verify it in the backend script
- [x] Usage example for SilverStripe 4.x/3.x
- [x] Usage example for Laravel
- [x] Usage example for Wordpress
How to display it on frontend page?
- Set up your Google Recaptcha v2 or v3 account for you website and get the site key and secret key
-
Import the GoogleRecaptcha class:
- Put below php code in your frontend template/page for Google Recaptcha v2.
Explain for above code:
- '\$SiteKey': The Google Recaptcha Site Key of your website. You can directly put site key here or a variable or from database.
- 'Form_ContactForm_Message': Form_Field_ID, via html code. eg. ...
<input type="text" id="Form_ContactForm_Message" />
... Your Google Recaptcha will display after this form field. - 'no_debug': Change to debug and not tick the I'm not a robot checkbox will continue submit the form, then you will see the failed message.
- 'mt-4 mb-1': Extra css class name for the Google Recaptcha area. For recaptcha v2: Add theme-dark if you want the dark theme instead of the light. Add no-api-js if you already import the recaptcha/api.js.
- 'Please tick the reCAPTCHA checkbox first': Frontend alert message if the end user does not tick the checkbox. Tips: You can change this value to "v3", it will automatically switch to use Google Recaptcha v3
- Default value of the parameters of the show() method
How to use Google Recaptcha Invisible
- You need get Google Recaptcha Invisible site key and secret key first. It's different from v2
- Based on above explain, add a sting invisible to the Extra css class will do the job. eg. mt-4 invisible mb-1
Frontend example for Google Recaptcha v3
- Our Google Recaptcha v3 will automatically get g-recaptcha-response value after the page load 10 seconds or the Form_Field_ID(eg. Form_ContactForm_Message) was clicked.
- 'no_debug': Change to "debug" will always submit an empty g-recaptcha-response to the backend.
- 'show-inline-badge mt-4 mb-3': Extra css class name for the Google Recaptcha inline-badge. Remove show-inline-badge will show a right bottom float Recaptcha badge instead inline-badge.
-
You need set up Google Recaptcha v3 site key and secret key first. It's different from v2
- If you do not want to use the show() method, You can also use your own code to display the recaptcha for a custom style. Just make sure the form action method is POST, then you can still use below verify() method in your backend script.
How to verify it in the backend script
-
Import the GoogleRecaptcha class:
-
Put below php code into the form-submitted-method() of your backend php file.
- Description for above code:
- '\$GoogleRecaptchaSecretKey': The Google Recaptcha Secret Key of your website. You can directly put secret key here or a variable or from database.
- 'Google Recaptcha Validation Failed': Javascript alert message if the verification failed. Set it null or false if you don't want a javascript alert. It will return true or false by the Google recaptcha verification result. Then you can show your own error message.
-
Default value of the parameters of the verify() method
- If you are using Google Recaptcha v3, it verify score < 0.5 as a failed result by default. You can set the score if you want a different value. eg.
Usage example for SilverStripe 4.x/3.x
-
Import the GoogleRecaptcha class:
-
Create a function to display the recaptcha in your controller. eg.:
-
Display the recaptcha in the frontend.ss form, add below code to the end of a frontend.ss template. eg. :
- Verify the recaptcha in the controller php file, add below code to the formAction function of your controller. eg.:
Usage example for Laravel 5.x custom login form
-
Include it in your LoginController.php file first:
-
Create a function to display the recaptcha in your LoginController.php eg.:
-
Display the recaptcha in the auth/login.blade.php, add below code to the end of the auth/login.blade.php template. eg. :
- Verify the recaptcha in the LoginController.php file, add below code for validateLogin. eg.:
Usage example for Wordpress custom form
-
Include it in your custom form template php file first. Note: Change the correct vendor path for require_once:
-
Display the recaptcha in the form template. eg. :
- Verify the recaptcha in the handle form submission method . eg.:
License
- MIT