recaptcha Invisible
<?php
$config = require('config.php');
?>
<html>
<head>
<title>reCAPTCHA demo</title>
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="process.php" method="POST" id="demo-form">
<input type="text" name="firstname">
<br>
<button class="loginmodal-submit g-recaptcha"
data-sitekey="6Lfi5B8UAAAAAGqVZbrz7-NHEEzn0SUBjL6l7wYD"
data-callback="onSubmit"
type="submit"
value="Submit">Submit
</button>
</form>
</body>
<script>
function onSubmit() {
document.getElementById("demo-form").submit();
}
</script>
</html>
// process.php:
<?php
require 'Recaptcha.php';
$recaptcha = $_POST['g-recaptcha-response'];
$object = new Recaptcha();
$response = $object->verifyResponse($recaptcha);
if(isset($response['success']) and $response['success'] != true) {
echo "An Error Occured and Error code is :".$response['error-codes'];
}
else {
echo "Correct Recaptcha";
}
?>
// Recaptcha.php:
<?php
class Recaptcha{
public function __construct(){
$this->config = require('config.php');
}
public function verifyResponse($recaptcha){
$remoteIp = $this->getIPAddress();
// Discard empty solution submissions
if (empty($recaptcha)) {
return array(
'success' => false,
'error-codes' => 'missing-input',
);
}
$getResponse = $this->getHTTP(
array(
'secret' => $this->config['secret-key'],
'remoteip' => $remoteIp,
'response' => $recaptcha,
)
);
// get reCAPTCHA server response
$responses = json_decode($getResponse, true);
if (isset($responses['success']) and $responses['success'] == true) {
$status = true;
} else {
$status = false;
$error = (isset($responses['error-codes'])) ? $responses['error-codes']
: 'invalid-input-response';
}
return array(
'success' => $status,
'error-codes' => (isset($error)) ? $error : null,
);
}
private function getIPAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
private function getHTTP($data){
$url = 'https://www.google.com/recaptcha/api/siteverify?'.http_build_query($data);
$response = file_get_contents($url);
return $response;
}
}
?>
// config.php
<?php
// these will work on Localhost
return [
'client-key' => '6Lfi5B8UAAAAAGqVZbrz7-NHEEzn0SUBjL6l7wYD',
'secret-key' => '6Lfi5B8UAAAAAIJdsbWfM3dvWjgxLjq4Iwycoypb'
];