<?php /*顯示原始碼*/ $file=__FILE__; require("src.php"); ?>
<?php
class ReCaptchaResponse
{
    public 
$success;
    public 
$errorCodes;
}
class 
ReCaptcha
{
    private static 
$_signupUrl "https://www.google.com/recaptcha/admin";
    private static 
$_siteVerifyUrl =
        
"https://www.google.com/recaptcha/api/siteverify?";
    private 
$_secret;
    private static 
$_version "php_1.0";
    
/**
     * Constructor.
     *
     * @param string $secret shared secret between site and ReCAPTCHA server.
     */
    
function ReCaptcha($secret)
    {
        if (
$secret == null || $secret == "") {
            die(
"To use reCAPTCHA you must get an API key from <a href='"
                
self::$_signupUrl "'>" self::$_signupUrl "</a>");
        }
        
$this->_secret=$secret;
    }
    
/**
     * Encodes the given data into a query string format.
     *
     * @param array $data array of string elements to be encoded.
     *
     * @return string - encoded request.
     */
    
private function _encodeQS($data)
    {
        
$req "";
        foreach (
$data as $key => $value) {
            
$req .= $key '=' urlencode(stripslashes($value)) . '&';
        }
        
// Cut the last '&'
        
$req=substr($req0strlen($req)-1);
        return 
$req;
    }
    
/**
     * Submits an HTTP GET to a reCAPTCHA server.
     *
     * @param string $path url path to recaptcha server.
     * @param array  $data array of parameters to be sent.
     *
     * @return array response
     */
    
private function _submitHTTPGet($path$data)
    {
        
$req $this->_encodeQS($data);
        
$response file_get_contents($path $req);
        return 
$response;
    }
    
/**
     * Calls the reCAPTCHA siteverify API to verify whether the user passes
     * CAPTCHA test.
     *
     * @param string $remoteIp   IP address of end user.
     * @param string $response   response string from recaptcha verification.
     *
     * @return ReCaptchaResponse
     */
    
public function verifyResponse($remoteIp$response)
    {
        
// Discard empty solution submissions
        
if ($response == null || strlen($response) == 0) {
            
$recaptchaResponse = new ReCaptchaResponse();
            
$recaptchaResponse->success false;
            
$recaptchaResponse->errorCodes 'missing-input';
            return 
$recaptchaResponse;
        }
        
$getResponse $this->_submitHttpGet(
            
self::$_siteVerifyUrl,
            array (
                
'secret' => $this->_secret,
                
'remoteip' => $remoteIp,
                
'v' => self::$_version,
                
'response' => $response
            
)
        );
        
$answers json_decode($getResponsetrue);
        
print_r($answers);
        
$recaptchaResponse = new ReCaptchaResponse();
        if (
trim($answers ['success']) == true) {
            
$recaptchaResponse->success true;
        } else {
            
$recaptchaResponse->success false;
            
$recaptchaResponse->errorCodes $answers ["error-codes"];
        }
        return 
$recaptchaResponse;
    }
}
?>
<div style="margin: 20px auto; width: 300px; border: 1px solid gray; padding: 20px 20px;">
<h2>reCAPTCHA</h2>
<p>實作reCAPTCHA</p>
<script src='https://www.google.com/recaptcha/api.js'></script>
<form action="/i11" method="POST">
       <label style="border: 1px solid purple; background-color: rgb(200,200,200); padding: 5px 10px;" ><input type="radio" name="gender" value="M">男</label>
       <label style="border: 1px solid purple; background-color: rgb(200,200,200); padding: 5px 10px;" ><input type="radio" name="gender" value="F">女</label><br><br><br>
    <div class="g-recaptcha" data-sitekey="6LdC-iUTAAAAAMN4MC1a-0LI7DEUxw-NGYXbd105"></div><br>

    <div style="float: right"><input type="submit"></div>
    <div style="clear: right"></div>
</form>
</div>
<h3>結果: </h3>

<pre><?php
if(empty($_POST)) {
    echo 
"無POST資訊.";
} else {
    
print_r($_POST);
    
// your secret key
    
$secret "6LdC-iUTAAAAADc6mc1ThzHOa_fXh3uFQifWc2SY";

    
// empty response
    
$response null;

    
// check secret key
    
$reCaptcha = new ReCaptcha($secret);

    if (
$_POST["g-recaptcha-response"]) {
    
$response $reCaptcha->verifyResponse(
        
$_SERVER["REMOTE_ADDR"],
        
$_POST["g-recaptcha-response"]
    );
    if (
$response != null && $response->success) {
        echo 
"驗證成功!";
      } else {
          echo 
"驗證失敗!";
      }


}
}
?></pre>

reCAPTCHA

實作reCAPTCHA





結果:

無POST資訊.