Skip to content

Instantly share code, notes, and snippets.

@kreativan
Last active May 10, 2024 07:25
Show Gist options
  • Save kreativan/720c4872f76bd235467dc820fe39d38d to your computer and use it in GitHub Desktop.
Save kreativan/720c4872f76bd235467dc820fe39d38d to your computer and use it in GitHub Desktop.
Google reCaptcha php Class
const formSubmitRecapcha = function (site_key = "") {
grecaptcha.ready(function () {
grecaptcha.execute(site_key, {
action: 'contact'
}).then(function (token) {
// Get #recaptchaResponse field and populate it's value
var recaptchaResponse = document.getElementById('recaptchaResponse');
recaptchaResponse.value = token;
//
// Make the Ajax Call
//
});
});
<?php
Use Kreativan\Recapcha;
?>
<form action="./" method="POST" enctype='multipart/form-data'>
<?php
echo Recapcha::input();
?>
<button type="button" onclick="formSubmitRecapcha('<?= Recapcha::get_site_key() ?>')">
Submit
</button>
</form>
<?php
use Kreativan\Recapcha;
/**
* Check for reCapcha
* if there is recaptcha_response in POST, validate it
*/
if (isset($_POST['recaptcha_response'])) {
$recapcha = Recapcha::is_valid();
if (!$recapcha) {
$response["status"] = "error";
$response["errors"][] = "Recapcha validation failed";
response($response);
exit();
}
}
<?php
/**
* Recapcha Class
* Handle recaptcha validation using $_POST['recaptcha_response'] request.
*
* You need to have "recaptcha_response" field in your form,
* and populate it's value dynamically just before or during the form submit.
*
* Set site_key and secret_key
* Recapcha::set_site_key('YOUR_CUSTOM_SITE_KEY');
* Recapcha::set_secret_key('YOUR_CUSTOM_SECRET_KEY');
*
* Render script in <head>
* @example echo Recapcha::script();
*
* Render input field inside form
* @example echo Recapcha::input();
*
* Validate - will return bool true/false
* @example $recapcha = Recapcha::is_valid();
*/
namespace Kreativan;
class Recapcha {
private static $site_key = '';
private static $secret_key = '';
/**
* GET
*/
public static function get_site_key() {
return self::$site_key;
}
public static function get_secret_key() {
return self::$secret_key;
}
/**
* SET
*/
public static function set_site_key($value) {
self::$site_key = $value;
}
public static function set_secret_key($value) {
self::$secret_key = $value;
}
public static function input() {
return '<input type="hidden" name="recaptcha_response" id="recaptchaResponse">';
}
/**
* Render google recaptcha script
*
* @example in <head> section of your website
* echo Kreativan\Recapcha::script();
*/
public static function script() {
return '<script async src="https://www.google.com/recaptcha/api.js?render=' . self::get_site_key() . '"></script>';
}
/**
* Check if recaptcha is valid
* @see @method self::validate()
*/
public static function is_valid() {
$validation_response = self::validate();
return $validation_response['success'];
}
/**
* Server side recapcha validation
* Send POST req to the google end-point with 'recaptcha_response'.
* End point will return json with 'success' key true/false
*/
public static function validate() {
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = self::secret_key();
$recaptcha_response = $_POST['recaptcha_response'];
// Make the POST request
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
return json_decode($recaptcha, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment