Google验证码无效

时间:2016-06-18 19:38:17

标签: php captcha recaptcha

我有一个php登录文件,我想将google验证码放在该登录表单中。我在一个文件中一起使用php和html(不将表单数据发送到其他php页面)。 问题是当用户不验证验证码时,他们仍然可以登录网站(即使验证码也不验证)。这是我的代码:

                    <form action="" method="post" id="form">                        
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-user"></i></span>
                            <input type="text" class="form-control" name="form-username" id="form-username" placeholder="Username" required="">
                        </div>
                    </div>                        
                    <div class="form-group">
                        <div class="input-group">
                            <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                            <input type="password" class="form-control" name="form-password" placeholder="Password" id="form-password" required="">
                        </div>
                    </div> 

  <div class="form-group">

                        <div class="g-recaptcha" data-theme="dark" data-sitekey="my site key"></div>

                    <div class="form-group no-border margin-top-20">
                     <input type="submit" name="submit" value="Sign In !" class="submit_button4 btn btn-primary btn-block" >

   </form>

<?php
    if($_SERVER["REQUEST_METHOD"] === "POST")
    {
        //form submitted

        //check if other form details are correct

        //verify captcha
        $recaptcha_secret = "my secret key";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
            echo "Logged In Successfully";
        }
        else
        {
            echo "You are a robot";
        }
    }
?>

注意:此代码<script src='https://www.google.com/recaptcha/api.js'></script>也在我的head标签中(所有代码都在一个页面中)。

1 个答案:

答案 0 :(得分:1)

如果验证失败,您需要设置类似header的内容。

if ($response["success"] === true) {
    echo "Captcha succeeded";
    // let's process our other login stuff
} else {
    echo "You are robot scum";
    header('Location: /');
    exit;
}

如果验证失败,目前,您没有做任何事情。简单地回应,然后继续。

您没有显示任何其他逻辑,让我对身份验证过程如何为您提供任何印象。

您必须将其集成到您的登录逻辑中,作为帖子的一部分,其中包含用户详细信息并将验证码失败作为验证失败的另一个原因(如提供错误的密码)。

相关问题