Download the PHP package germania-kg/googlerecaptcha without Composer
On this page you can find all versions of the php package germania-kg/googlerecaptcha. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download germania-kg/googlerecaptcha
More information about germania-kg/googlerecaptcha
Files in germania-kg/googlerecaptcha
Package googlerecaptcha
Short Description Callable and Pimple Service Provider for Google's ReCaptcha.
License MIT
Informations about the package googlerecaptcha
Germania KG · GoogleRecaptcha
Callable wrapper, Slim3 Middleware and Pimple-style Service Provider for Google's ReCaptcha.
Installation with Composer
Alternatively, add this package directly to your composer.json:
Usage
The following examples assume you're working with Pimple or Slim Framework and have your DI container at hand:
ServiceProvider
See chapter reCAPTCHA v2 FAQ.
Slim3-style Middleware
Before the route controller is executed, this middleware checks
- whether there is a recaptcha user input in
$_POST['g-recaptcha-response']
- whether the validation with Google's ReCaptcha client succeeds.
Dependent on the results, this middleware does
- Store the validation result in a Request attribute named
GoogleRecaptcha
. - When the validation fails, add a
400 Bad Request
status code to the Response object.
The string identifiers used here can be modified in the Google.Recaptcha.Config service, see below.
IMPORTANT NOTICE: The middleware will call the $next middleware, regardless of the validation status. Any route controller should check for the HTTP status itself and react accordingly. The GoogleRecaptcha
Request attribute array will help – just ask for failed or success or status elements.
Callable validation wrapper
The ReCaptcha validation client is instantiated automatically. The callable wrapper uses the same logger instance than the Google.Recaptcha.Validator on how to setup your own validator instance.
The services in detail
Google.Recaptcha.PublicKey
This is the service you surely will need most often.
Google.Recaptcha.Logger
The default logger has been passed on instantiation. Override or customize like this:
Google.Recaptcha.ClientIP
The client API is used to ask Googles web API; its default is $_SERVER['REMOTE_ADDR']
. You normally will not need to override this:
Google.Recaptcha.Validator
This creates Google's server-side validation client which comes with the official ReCaptcha\ReCaptcha library. It will be automatically installed with this GoogleRecaptcha package and automatically instantiated. If you wish to create your own, do something like:
Google.Recaptcha.Validator.Callable
is a callable wrapper, i.e. an invokable class, around the Google.Recaptcha.Logger instance from above, logging an info on success and a notice on failure.
When instantiated manually, it accepts an optional logger.
Google.Recaptcha.Config
This configuration array is used by the GoogleRecaptchaMiddleware and provides these values:
field | value | description |
---|---|---|
input_field | g-recaptcha-response |
The form field name |
status_code | 400 |
The Response status when validation fails |
request_attribute | GoogleRecaptcha |
The Request attribute name for validation information |
Development
Unit tests
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
All versions of googlerecaptcha with dependencies
pimple/pimple Version ^3.0
google/recaptcha Version ^1.1
psr/log Version ^1.0