Created
April 10, 2017 18:21
-
-
Save marcosnakamine/870642aa21bdee13bd4b1523a91bfd32 to your computer and use it in GitHub Desktop.
PHP - Google Invisible reCAPTCHA example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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
I thinks better was add
'remoteip' => $_SERVER['REMOTE_ADDR'],
in http request.