ReCaptcha For Newbies

时间:2016-06-27 16:21:22

标签: recaptcha

我有ReCaptcha工作,但尽管阅读了文档和这里发布的答案,我仍然不知道设置服务器端。我的HTML表单调用<form id="contactForm" class="well" method="POST" action="php/contactform.php">

我将服务器端recaptcha放在此文件中的位置和位置是什么? (我的意思是当我标题这个新手。我真的需要明确的指示):

<?php
if($_POST){
// response hash
$response = array('message'=>''); 
}

try {       
    // Get values from form 
    $name=$_POST['cname'];
    $email=$_POST['cemail'];
    $subject=$_POST['csubject'];
    $message=$_POST['cmessage'];


    $formcontent="From: $name \n Email: $email \n Subject: $subject \n: $message";
    $recipient = "rabbidubrow@fivegates.org";
    $subject = "KHF Contact Form";
    $mailheader = "From: $email \r\n";
    $send_contact=mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");   

    // let's assume everything is ok, setup successful response
        $response['type'] = 'success';
        $response['message'] = 'Thank you! We will be in touch shortly.';   
    } catch(Exception $e){
        $response['type'] = 'error';
        $response['message'] = $e->getMessage();
    }
    // now we are ready to turn this hash into JSON
    print json_encode($response);
    exit;
?> 

1 个答案:

答案 0 :(得分:0)

你需要
 1.包括你的recaptcha.php
 2.声明您的私钥和公钥  3.检查验证码的POST。如果成功,则给出响应,如果失败,则捕获异常。

以下是我的一个脚本,供您参考。

require_once('assets/config/recaptchalib.php');

$publickey = "xxxx";
$privatekey = "xxxxx";


if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
        if ($resp->is_valid) {
            $continue = true;
        }
}