Skip to content

Instantly share code, notes, and snippets.

@leorsida
Forked from marcosnakamine/index.php
Created January 31, 2018 08:51
Show Gist options
  • Save leorsida/d65c99d0bd7c1cd18a05578e33edcce3 to your computer and use it in GitHub Desktop.
Save leorsida/d65c99d0bd7c1cd18a05578e33edcce3 to your computer and use it in GitHub Desktop.
PHP - Google Invisible reCAPTCHA example
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("demo-form").submit();
}
</script>
</head>
<body>
<?php
if ( isset($_POST['envia']) && $_POST['envia'] == 'ok' ) {
$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( array(
'response' => $_POST['g-recaptcha-response'],
'secret' => 'secret_key'
) ),
),
) ) );
$result = json_decode($result);
var_dump( $result->success );
}
?>
<form id='demo-form' action="?" method="POST">
<input type="text" name="teste">
<input type="hidden" name="envia" value="ok">
<button class="g-recaptcha" data-sitekey="site_key" data-callback='onSubmit'>Submit</button>
<br/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment