{"id":16462702,"url":"https://github.com/alexstack/google-recaptcha-to-any-form","last_synced_at":"2025-03-23T10:34:05.237Z","repository":{"id":56944842,"uuid":"193593307","full_name":"AlexStack/Google-Recaptcha-to-any-form","owner":"AlexStack","description":"Add a Google Recaptcha v2 or v3 or invisible to any custom form with flexible settings and no affection to your existing code. Also works well for SilverStripe 4.x/3.x/2.x \u0026 Larave \u0026 Wordpress \u0026 other CMS. No need jQuery.","archived":false,"fork":false,"pushed_at":"2019-09-03T06:04:37.000Z","size":119,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-12T11:12:09.085Z","etag":null,"topics":["googlerecaptcha","recaptcha","silverstripe"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexStack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-24T23:07:11.000Z","updated_at":"2023-05-18T15:54:15.000Z","dependencies_parsed_at":"2022-08-21T02:40:22.157Z","dependency_job_id":null,"html_url":"https://github.com/AlexStack/Google-Recaptcha-to-any-form","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexStack%2FGoogle-Recaptcha-to-any-form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexStack%2FGoogle-Recaptcha-to-any-form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexStack%2FGoogle-Recaptcha-to-any-form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexStack%2FGoogle-Recaptcha-to-any-form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexStack","download_url":"https://codeload.github.com/AlexStack/Google-Recaptcha-to-any-form/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221849560,"owners_count":16891490,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["googlerecaptcha","recaptcha","silverstripe"],"created_at":"2024-10-11T11:12:06.560Z","updated_at":"2024-10-28T15:37:14.781Z","avatar_url":"https://github.com/AlexStack.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Google Recaptcha v2 or v3 to any form\n[![Latest Stable Version](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/v/stable)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)\n[![License](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/license)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)\n[![Total Downloads](https://poser.pugx.org/alexstack/google-recaptcha-to-any-form/downloads)](https://packagist.org/packages/alexstack/google-recaptcha-to-any-form)\n\n- 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 \u0026 Larave \u0026 Wordpress \u0026 other CMS.\n\n# Basic example codes\n\n- Display Google Recaptcha v2 or v3 after a Form_Field_ID:\n\n```php\n// For Google recaptcha v2\nGoogleRecaptcha::show('SiteKey', 'Form_Field_ID');\n\n// For Google recaptcha v3\nGoogleRecaptcha::showV3('SiteKey', 'Form_Field_ID');\n```\n\n- Verify it in the backend php:\n\n```php\nGoogleRecaptcha::verify('SecretKey');\n```\n\n# How to install\n\n```php\ncomposer require alexstack/google-recaptcha-to-any-form\n```\n\n# Contents\n\n- [x] [How to display it on frontend page](#frontend) - Support [Google recaptcha v3](#v3) / [v2](#frontend) / [Invisible](#invisible)\n- [x] [How to verify it in the backend script](#backend)\n- [x] [Usage example for SilverStripe 4.x/3.x](#silverstripe)\n- [x] [Usage example for Laravel](#laravel)\n- [x] [Usage example for Wordpress](#wordpress)\n\n# \u003ca name=\"frontend\"\u003e\u003c/a\u003eHow to display it on frontend page?\n\n![How to display it on frontend page](https://developers.google.com/recaptcha/images/newCaptchaAnchor.gif \"Google Recaptcha\")\n\n- Set up your Google Recaptcha v2 or v3 account for you website and get the site key and secret key\n- Import the GoogleRecaptcha class:\n\n```php\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n```\n\n- Put below php code in your frontend template/page for **Google Recaptcha v2**.\n\n```php\nGoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');\n```\n![google-recaptcha-v2-default](docs/images/google-recaptcha-v2-default.png \"google-recaptcha-v2-default\")\n\n### Explain for above code:\n  - '\\$SiteKey': The Google Recaptcha Site Key of your website. You can directly put site key here or a variable or from database.\n  - 'Form_ContactForm_Message': Form_Field_ID, via html code. eg. ... `\u003cinput type=\"text\" id=\"Form_ContactForm_Message\" /\u003e` ... Your Google Recaptcha will display after this form field.\n  - '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.\n  - '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.\n  - '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\n- Default value of the parameters of the show() method\n\n```php\nshow($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class=\"mt-4 mb-4\", $please_tick_msg=\"Please tick the I'm not robot checkbox\");\n```\n\n### \u003ca name=\"invisible\"\u003e\u003c/a\u003eHow to use **Google Recaptcha Invisible**\n- You need get Google Recaptcha Invisible site key and secret key first. It's different from v2\n- Based on above explain, add a sting **invisible** to the Extra css class will do the job. eg. mt-4 invisible mb-1\n```php\nGoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','mb-2 invisible');\n```\n\n### \u003ca name=\"v3\"\u003e\u003c/a\u003eFrontend example for **Google Recaptcha v3**\n```php\nGoogleRecaptcha::show($SiteKey, 'Form_Field_ID', 'no_debug','show-inline-badge mt-4 mb-3','v3');\n// or\nGoogleRecaptcha::showV3($SiteKey, 'Form_Field_ID', 'no_debug');\n```\n![google-recaptcha-v3-default](docs/images/google-recaptcha-v3-default.png \"google-recaptcha-v3-default\")\n\n- 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.\n- 'no_debug': Change to \"debug\" will always submit an empty g-recaptcha-response to the backend.\n- '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.\n\n![google-recaptcha-v3-inline](docs/images/google-recaptcha-v3-inline.png \"google-recaptcha-v3-inline\")\n- You need set up Google Recaptcha v3 site key and secret key first. It's different from v2\n\n- 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.\n\n\n# \u003ca name=\"backend\"\u003e\u003c/a\u003eHow to verify it in the backend script\n\n- Import the GoogleRecaptcha class:\n\n```php\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n```\n\n- Put below php code into the form-submitted-method() of your backend php file.\n\n```php\nGoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');\n```\n\n- Description for above code:\n  - '\\$GoogleRecaptchaSecretKey': The Google Recaptcha Secret Key of your website. You can directly put secret key here or a variable or from database.\n  - '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.\n- Default value of the parameters of the verify() method\n\n```php\nverify($secret_key, $break_msg = null, $recaptcha_score=0.5)\n```\n\n- If you are using Google Recaptcha v3, it verify score \u003c 0.5 as a failed result by default. You can set the score if you want a different value. eg.\n\n```php\nGoogleRecaptcha::verify($SecretKey, 'Google Recaptcha Validation Failed!!', 0.36);\n```\n\n# \u003ca name=\"silverstripe\"\u003e\u003c/a\u003eUsage example for SilverStripe 4.x/3.x\n\n- Import the GoogleRecaptcha class:\n\n```php\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n```\n\n- Create a function to display the recaptcha in your controller. eg.:\n\n```php\npublic function showGoogleRecaptcha()   {\n    return GoogleRecaptcha::show($SiteKey, 'Form_ContactForm_Message', 'no_debug', 'mt-4 mb-1', 'Please tick the reCAPTCHA checkbox first!');\n}\n```\n\n- Display the recaptcha in the frontend.ss form, add below code to the end of a frontend.ss template. eg. :\n\n```php\n$showGoogleRecaptcha.RAW\n```\n\n- Verify the recaptcha in the controller php file, add below code to the formAction function of your controller. eg.:\n\n```php\nGoogleRecaptcha::verify($GoogleRecaptchaSecretKey, 'Google Recaptcha Validation Failed!!');\n```\n\n# \u003ca name=\"laravel\"\u003e\u003c/a\u003eUsage example for Laravel 5.x custom login form\n\n- Include it in your LoginController.php file first:\n\n```php\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n```\n\n- Create a function to display the recaptcha in your LoginController.php eg.:\n\n```php\npublic function showLoginForm()\n{\n    $showRecaptcha = GoogleRecaptcha::show('site_key', 'password', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');\n    return view('auth.login', compact('showRecaptcha'));\n}\n```\n\n- Display the recaptcha in the auth/login.blade.php, add below code to the end of the auth/login.blade.php template. eg. :\n\n```php\n{!! $showRecaptcha !!}\n```\n\n- Verify the recaptcha in the LoginController.php file, add below code for validateLogin. eg.:\n\n```php\nprotected function validateLogin(Request $request)\n{\n\n    GoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');\n\n    $request-\u003evalidate([\n        $this-\u003eusername() =\u003e 'required|string',\n        'password' =\u003e 'required|string',\n    ]);\n}\n```\n\n# \u003ca name=\"wordpress\"\u003e\u003c/a\u003eUsage example for Wordpress custom form\n\n- Include it in your custom form template php file first. Note: Change the correct vendor path for require_once:\n\n```php\nrequire_once(__DIR__ . '/../../../../vendor/autoload.php');\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n```\n\n- Display the recaptcha in the form template. eg. :\n\n```php\necho GoogleRecaptcha::show('site_key', 'input_2_3', 'no_debug', 'mt-4 mb-3 col-md-6 offset-md-4', 'Please tick the reCAPTCHA checkbox first!');\n```\n\n- Verify the recaptcha in the handle form submission method . eg.:\n\n```php\nrequire_once(__DIR__ . '/../../vendor/autoload.php');\nuse GoogleRecaptchaToAnyForm\\GoogleRecaptcha;\n\nGoogleRecaptcha::verify('secret_key', 'Google Recaptcha Validation Failed!!');\n```\n\n# License\n\n- MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexstack%2Fgoogle-recaptcha-to-any-form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexstack%2Fgoogle-recaptcha-to-any-form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexstack%2Fgoogle-recaptcha-to-any-form/lists"}