Google ReCaptcha Display with Image&表单上的客户端验证提交

时间:2016-10-21 02:40:23

标签: php validation client-side recaptcha

我已经关注

How to Validate Google reCaptcha on Form Submit

我的 index.php

中有以下代码
<!DOCTYPE HTML>
<html>
    <head>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
        <form method="post" action="post.php">
            <div class="g-recaptcha" data-sitekey="6XXXXXXXXXXXXwdsf0K8HbXXXXXXX"></div>
            <input type="submit" value="Submit" />
        </form>
    </body>
</html>

post.php中

$response = $_REQUEST['g-recaptcha-response'];
$secret = '6XXXXXXXXXXXXwdsf0K8HbJNvMw-XXXX';
$server = $_SERVER['REMOTE_ADDR'];

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $response.'&remoteip='.$server);
$response = json_decode($response, true);
if ($response["success"] === true) {
    echo "Logged In Successfully";
} else {
    echo "You are a robot";
}
exit;

以上代码工作正常。

如何进行客户端验证?它没有显示带有图像选项的Captcha。

enter image description here

我已经在下面做了

enter image description here

1 个答案:

答案 0 :(得分:1)

这是Recaptcha库的标准行为,第一次不显示对图像的控制。

尝试多次查看或发布页面,您会看到图片最终没有出现。

如果要在其他附加字段上进行某些客户端验证,则必须使用jQuery或标准库,如bootstrap或foundation,如thisthis。您可以在此处看到工作脚本的完整示例(灵感来自引导脚本和HTML 5功能):

此版本的脚本在整个Internet上都是相同的。没有更多客户端验证!请参阅参考:codepen.io signup

例如:

<!DOCTYPE HTML>
<html>
    <head>
      <link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
      <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
        <form class="signin-form" method="post" action="post.php">
            <!-- for example : Email and password validation (HTML 5) -->
            <label for="inputEmail" class="sr-only">Email address</label>
            <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
            <label for="inputPassword" class="sr-only">Password</label>
            <input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
            <!-- Site-key for automated tests -->
            <div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>
    </body>
</html>

here示例代码笔。