Skip to content

Instantly share code, notes, and snippets.

@bactisme
Last active July 18, 2016 14:49
Show Gist options
  • Save bactisme/824009d68e4434f9c100304bb9da3962 to your computer and use it in GitHub Desktop.
Save bactisme/824009d68e4434f9c100304bb9da3962 to your computer and use it in GitHub Desktop.
Extract a ReCapcha challenge to be presented to a external user
<?php
/*
PHP template to ask a recapcha challenge, present it to a user.
*/
<?php
class BustRecapcha {
public function __construct(){
$this->recapcha_key = "EDIT_HERE_WITH_OBSERVED_KEY";
if (!isset($_GET['recaptcha_response_field']) && !isset($_POST['recaptcha_response_field'])){
$r = $this->callNoScriptPage();
if (is_array($r) && !empty($r)){
$this->makeForm($r);
}
}else{
// info will be in
}
}
function callNoScriptPage(){
$result = array();
$url = "https://www.google.com/recaptcha/api/noscript?k=".$this->recapcha_key;
$html = file_get_contents($url);
preg_match('/src\="(.*)"/', $html, $matches);
$result['image_url'] = "https://www.google.com/recaptcha/api/".$matches[1];
preg_match('/value\="(.*)"/', $html, $matches);
$result['recaptcha_challenge_field'] = $matches[1];
return $result;
}
function makeForm($challenge_data){
?>
<img src="<?php echo $challenge_data['image_url'] ?>">
<input name="recaptcha_challenge_field" id="recaptcha_challenge_field" type="hidden" value="<?php echo $challenge_data['recaptcha_challenge_field']; ?>">
<input name="recaptcha_response_field" id="recaptcha_response_field" type="text" autocomplete="off">
<script>
$("#recaptcha_response_field").keyup(function (e){
if (e.keyCode == 13) {
//console.log($("#recaptcha_challenge_field").val());
//console.log($("#recaptcha_response_field").val());
// RE DO REQUEST HERE, WITH recaptcha_challenge_field and recaptcha_response_field POST FIELDS
}
});
</script>
<?php
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment