如何在我的自定义动态PHP网站中实现reCAPTCHA?

时间:2015-06-24 14:30:14

标签: php html recaptcha

我选择了这个脚本,并且检查/验证了reCAPTCHA的工作原理。我迷失在那里,我必须添加PHP命令"邮件到"通过邮件获取联系表格。请参阅下面的代码。我必须在

之后将"邮件添加到" -stuff
if($response->success) {

正确?但我不知道这是怎么回事。 Evrytime我尝试s.th.我只得到"试图获得非对象的属性"对于" if($ response-> success){"是

<form class="form-horizontal" action="" name="contactForm" method="post">

<input type="text" name="nickname" class="input_nickname" placeholder="Name" required="required"/>
<input type="email" name="mail" class="input_mail" placeholder="Mail" required="required"/>
<input type="text" name="remotekey" class="input_key" placeholder="REMOTE private key (optional)"/>
<textarea type="text" name="message" class="input_message" placeholder="Message" required="required"/></textarea>


<?php
// google recapcha keys and url
$siteKey = '6LdU2wQTAAAAAN_tSEUEhRPcIc8hc7VpXrKAS7R8';
$secretKey = '6LdU2wQTAAAAABPtl36ax3yHeKCiOKOFfFbAHitJ';
$recaptchaVerifyURL = "https://www.google.com/recaptcha/api/siteverify";

// send request using CURL
function curlRequest($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
        $curlData = curl_exec($curl);
        curl_close($curl);
        return $curlData;
}
// get user ip address
function getClientIP() {
     if (isset($_SERVER['HTTP_CLIENT_IP']))
         $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
     else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
     else if(isset($_SERVER['HTTP_X_FORWARDED']))
         $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
     else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
     else if(isset($_SERVER['HTTP_FORWARDED']))
         $ipaddress = $_SERVER['HTTP_FORWARDED'];
     else if(isset($_SERVER['REMOTE_ADDR']))
         $ipaddress = $_SERVER['REMOTE_ADDR'];
     else
         $ipaddress = 'UNKNOWN';

     return $ipaddress; 
}

// process form request
if(($_SERVER["REQUEST_METHOD"] == "POST") && isset($_POST['submit'])) {

    $recaptchaUserInput = $_POST['g-recaptcha-response'];

    if(empty($recaptchaUserInput)) {

        echo 'Please enter your reCAPTCHA'; 

    } else {

        $userIP = getClientIP();

        $recaptchaVerifyURL .= '?'.http_build_query(array('secret' => $secretKey,'response' => $recaptchaUserInput,'remoteip' => $userIP)); 

        $response = curlRequest($recaptchaVerifyURL);

        $response = json_decode($response);


        if($response->success) {



            // valid request
            // send contact email into to admin via email  or store contact info in database


        } else {
            echo 'Please re-enter your reCAPTCHA';  
        }
    } 

}
?>


<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
<button name="submit" type="submit" class="btn btn-default">Submit</button>
</form>

0 个答案:

没有答案