Skip to content

Instantly share code, notes, and snippets.

@marcosnakamine
Created April 10, 2017 18:21
Show Gist options
  • Save marcosnakamine/870642aa21bdee13bd4b1523a91bfd32 to your computer and use it in GitHub Desktop.
Save marcosnakamine/870642aa21bdee13bd4b1523a91bfd32 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>
@NabiKAZ
Copy link

NabiKAZ commented Aug 16, 2017

I thinks better was add 'remoteip' => $_SERVER['REMOTE_ADDR'], in http request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment