在本地测试reCAPTCHA。在Mac上验证,但在Windows上验证

时间:2017-06-21 15:17:41

标签: php recaptcha

我正在两个操作系统上本地测试reCAPTCHA。生成的密钥用于域localhost和127.0.0.1。

在我的Mac上,SAME EXACT文件完美无缺。我将所述文件复制到我的Windows计算机上,我的响应总是返回NULL。

HTML

<!DOCTYPE html>
<html>
<head>
<title>This is a test</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>

<form action="/form.php" method="post">
    <input type="text" name="name" placeholder="name"><br>
    <input type="text" name="phone" placeholder="phone"><br>
    <div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

PHP(SERVER)

<?php 

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
    $privatekey = "SECRET_KEY";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => $privatekey,
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    $curlConfig = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data
    );

    $ch = curl_init();
    curl_setopt_array($ch, $curlConfig);
    $response = curl_exec($ch);
    curl_close($ch);
}

$jsonResponse = json_decode($response);

if ($jsonResponse->success === true) {
    echo '<br><p>CAPTCHA was completed successfully!</p><br>';
}
else {
   echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
}

?>

感谢所提供的任何帮助。

0 个答案:

没有答案