Last active
August 29, 2015 13:57
-
-
Save hugodias/9389326 to your computer and use it in GitHub Desktop.
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
<?php | |
require_once('recaptchalib.php'); | |
// Get a key from https://www.google.com/recaptcha/admin/create | |
$publickey = "6Ld6m-8SAAAAAGNrxIf7Dn6rt_6yfjcMO40Zxs_G"; | |
$privatekey = "6Ld6m-8SAAAAAOLkKK6T8j4K9YntpGA0f-xszASW"; | |
$token = 'VopcWwRNfnCzUhmgaXM7sbQBurqixY59yFvt1J8ZkjeD2AK4SH36TPLdEG'; | |
$referer = "http://www.verscontabilidade.com.br"; | |
$url_back = "http://www.verscontabilidade.com.br/obrigado"; | |
# the response from reCAPTCHA | |
$resp = null; | |
# the error code from reCAPTCHA, if any | |
$error = null; | |
function sendMessage($post, $publickey, $privatekey, $token, $referer, $url_back){ | |
if($post['recaptcha_response_field']){ | |
if(verifyCaptcha($privatekey, $post)){ | |
$nome = $_POST['nome']; | |
$email = $_POST['email']; | |
$assunto = $_POST['assunto']; | |
$mensagem = $_POST['mensagem']; | |
$url = "http://enviarformularios.com.br/send"; | |
$fields = array( | |
'nome' => $nome, | |
'email' => $email, | |
'assunto' => $assunto, | |
'mensagem' => $mensagem, | |
'token' => $token | |
); | |
//url-ify the data for the POST | |
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } | |
rtrim($fields_string,'&'); | |
//open connection | |
$ch = curl_init(); | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch,CURLOPT_URL,$url); | |
curl_setopt($ch,CURLOPT_POST,count($fields)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); | |
curl_setopt($ch,CURLOPT_AUTOREFERER,true); | |
curl_setopt($ch,CURLOPT_REFERER, $referer); | |
//execute post | |
$result = curl_exec($ch); | |
if($url_back){ | |
header("location: ".$url_back); | |
} else { | |
echo "<script>alert('Formulario enviado com sucesso!')</script>"; | |
} | |
} else { | |
echo "<script>alert('Captcha incorreto.')</script>"; | |
} | |
} else { | |
echo "<script>alert('Favor preencher o captcha')</script>"; | |
} | |
} | |
function verifyCaptcha( $privatekey, $post ) { | |
$resp = recaptcha_check_answer ($privatekey, | |
$_SERVER["REMOTE_ADDR"], | |
$post["recaptcha_challenge_field"], | |
$post["recaptcha_response_field"]); | |
return $resp->is_valid; | |
} | |
if ($_POST){ | |
sendMessage($_POST,$publickey,$privatekey,$token,$referer,$url_back); | |
} | |
?> | |
... | |
<form method="POST"> | |
... | |
<?php | |
# Captcha | |
echo recaptcha_get_html($publickey, $error); | |
?> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment