dev-master
9999999-devMakes integrating Google's ReCaptcha simple!
MIT
Makes integrating Google's ReCaptcha simple!
A wrapper class for ReCaptcha to make integrating into your site dead simple., (*1)
Create an account on Google and navigate to their ReCaptcha service here:, (*2)
https://www.google.com/recaptcha/admin
Register a new site, then keep the tab open with your Site Key and Secret visible., (*3)
Include PHPRecaptcha in your site with composer:, (*4)
composer require htmlguyllc/phprecaptcha
Either Copy the google_credentials.example.ini and create a file named google_credentials.ini in the same folder:, (*5)
OR set your credentials in environment variables with the following keys:, (*6)
OR store your credentials however you want, and pass them during instantiation:, (*7)
$captcha = new \HTMLGuyLLC\ReCaptcha($secret, $site_key);
Include the following in your HTML:, (*8)
<script src='https://www.google.com/recaptcha/api.js'></script>
Use the following where you want to display the captcha (presumably in a form):, (*9)
$captcha = new \HTMLGuyLLC\ReCaptcha(); echo $captcha->display();
To validate a captcha after it's been completed and the form has been posted, run the verify() method., (*10)
try { $captcha = new \HTMLGuyLLC\ReCaptcha(); if( !$captcha->verify() ) { //user failed to complete the captcha correctly } } catch(\HTMLGuyLLC\ReCaptchaExeption $e) { //do something specific for errors with recaptcha or this class } catch(\Exception $e) { //catch any unexpected exceptions }
In Javascript, if you use AJAX to submit the form, you'll want to regenerate the captcha. Use the following in your AJAX complete callback:, (*11)
grecaptcha.reset();
$(document).ajaxComplete(function(event,request,settings){ if( typeof grecaptcha !== 'undefined' ) { grecaptcha.reset() } });
PHP, CURL, ReCaptcha, Guzzle (optional), (*12)
Initial, (*13)
Include support for environment variables, (*14)
Allow override of curl verify host/peer options in verify() method, (*15)
MIT License, (*16)
Copyright (c) 2018 Shane Stebner, (*17)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*18)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*19)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*20)
Makes integrating Google's ReCaptcha simple!
MIT