如何在php中使用google ReCAPTCHA

如何在php中使用google ReCAPTCHA,php,html,css,Php,Html,Css,我有一个PHP表单,其中包含一些字段和GoogleRecaptCha字段以及存储在数据库表中的值,但是我想让GoogleRecaptCha字段成为必需的。 代码如下: <?PHP if(isset($_POST['submit'])) { { $name=$_REQUEST['name']; $email=$_REQUEST['email']; $message=$_REQUEST['message']; $user

我有一个PHP表单,其中包含一些字段和GoogleRecaptCha字段以及存储在数据库表中的值,但是我想让GoogleRecaptCha字段成为必需的。 代码如下:

<?PHP

if(isset($_POST['submit']))
    {
{
        $name=$_REQUEST['name'];
        $email=$_REQUEST['email'];
        $message=$_REQUEST['message'];
        $user_id=$_SESSION['id'];   
        $sql="insert into contact (name,email,message,user_id,status)
        values('$name','$email','$message','$user_id','1')";
        $qex=mysql_query($sql); 
        if(!$qex)
        {
            die("Contact information  is not Added".mysql_error());     
        }   
        $msgsec="Contact information is Added";
?>

这是带有验证的谷歌验证码

 <html>
<script src='https://www.google.com/recaptcha/api.js'></script>
<?PHP

/*Site Key and secret key is different thing so change it with ur keys */
$errMsg ="";
if(isset($_POST['submit']))
{
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key

        $secret = '*********************';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        print_r($responseData);

        $name = !empty($_POST['name'])?$_POST['name']:'';
        $email = !empty($_POST['email'])?$_POST['email']:'';
        $message = !empty($_POST['message'])?$_POST['message']:'';
        $user_id=$_SESSION['id'];   
        if($responseData->success):
            //contact form submission code
            $sql="insert into contact (name,email,message,user_id,status)
            values('$name','$email','$message','$user_id','1')";
            $qex=mysql_query($sql); 
            if(!$qex)
            {
                die("Contact information  is not Added".mysql_error());     
            }   
            $errMsg="Contact information is Added";

        else:
            $errMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $errMsg = 'Please click on the reCAPTCHA box.';
    endif;

}
echo $errMsg ;
?>
<body>
<form id='contactus' method='post' >
        <input type='hidden' name='submit' id='submit' value='1'/>
        <label><h2>Your Name <strong style="color:red">*</strong></h2></label>
            <input type="text" class="form-control" required name="name" id="name" placeholder="Please enter you'r name"/>
        <label><h2>Your Email <strong style="color:red">*</strong></h2></label>
            <input type="email" class="form-control" required name="email" id="email" placeholder="Please enter you'r email address"/>
        <label><h2>Your Message <strong style="color:red">*</strong></h2></label>
            <textarea class="form-control" required name="message" id="message" placeholder="Please type you'r message here"></textarea>
        <br  />
    <div class='container'>
        <div class="g-recaptcha" data-sitekey="*********************"></div>
    </div>
    <div class='container'>
        <input type="submit" name="submit" class="btn btn-primary" value="SEND MESSAGE">
    </div>  
  </form>
</body>  
</html>


这里是谷歌验证码与验证

 <html>
<script src='https://www.google.com/recaptcha/api.js'></script>
<?PHP

/*Site Key and secret key is different thing so change it with ur keys */
$errMsg ="";
if(isset($_POST['submit']))
{
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key

        $secret = '*********************';
        //get verify response data
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        print_r($responseData);

        $name = !empty($_POST['name'])?$_POST['name']:'';
        $email = !empty($_POST['email'])?$_POST['email']:'';
        $message = !empty($_POST['message'])?$_POST['message']:'';
        $user_id=$_SESSION['id'];   
        if($responseData->success):
            //contact form submission code
            $sql="insert into contact (name,email,message,user_id,status)
            values('$name','$email','$message','$user_id','1')";
            $qex=mysql_query($sql); 
            if(!$qex)
            {
                die("Contact information  is not Added".mysql_error());     
            }   
            $errMsg="Contact information is Added";

        else:
            $errMsg = 'Robot verification failed, please try again.';
        endif;
    else:
        $errMsg = 'Please click on the reCAPTCHA box.';
    endif;

}
echo $errMsg ;
?>
<body>
<form id='contactus' method='post' >
        <input type='hidden' name='submit' id='submit' value='1'/>
        <label><h2>Your Name <strong style="color:red">*</strong></h2></label>
            <input type="text" class="form-control" required name="name" id="name" placeholder="Please enter you'r name"/>
        <label><h2>Your Email <strong style="color:red">*</strong></h2></label>
            <input type="email" class="form-control" required name="email" id="email" placeholder="Please enter you'r email address"/>
        <label><h2>Your Message <strong style="color:red">*</strong></h2></label>
            <textarea class="form-control" required name="message" id="message" placeholder="Please type you'r message here"></textarea>
        <br  />
    <div class='container'>
        <div class="g-recaptcha" data-sitekey="*********************"></div>
    </div>
    <div class='container'>
        <input type="submit" name="submit" class="btn btn-primary" value="SEND MESSAGE">
    </div>  
  </form>
</body>  
</html>


在php文件中添加以下内容

if(isset($_POST['submit']))
{
 if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    $user_id=$_SESSION['id']; 

    $sql="insert into contact (name,email,message,user_id,status)
    values('$name','$email','$message','$user_id','1')";
    $qex=mysql_query($sql); 
    if(!$qex)
    {
        die("Contact information  is not Added".mysql_error());     
    }   
    $msgsec="Contact information is Added";
      }
    else
    {
    //your message for select recaptcha or required
    }
  }

在php文件中添加以下内容

if(isset($_POST['submit']))
{
 if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $message=$_REQUEST['message'];
    $user_id=$_SESSION['id']; 

    $sql="insert into contact (name,email,message,user_id,status)
    values('$name','$email','$message','$user_id','1')";
    $qex=mysql_query($sql); 
    if(!$qex)
    {
        die("Contact information  is not Added".mysql_error());     
    }   
    $msgsec="Contact information is Added";
      }
    else
    {
    //your message for select recaptcha or required
    }
  }

Ajax将是一个有效的解决方案。可能重复的sir可能重复此表单将由客户端填写&客户端也将选择google RECAPTCHA,但google RECAPTCHA可以访问此表单&所有表单值将存储在DB表上。非常好。。。只需查看Rohan或Mittul添加的链接。一定要把它添加到你的代码中,我相信如果你尝试的话,你会在几分钟内解决它!Ajax将是一个有效的解决方案。可能重复的sir可能重复此表单将由客户端填写&客户端也将选择google RECAPTCHA,但google RECAPTCHA可以访问此表单&所有表单值将存储在DB表上。非常好。。。只需查看Rohan或Mittul添加的链接。一定要把它添加到你的代码中,我相信如果你尝试的话,你会在几分钟内解决它!先生,我已经检查过了,请尝试一下,但是没有显示在db表中存储表单值的方法,您必须在成功条件下存储值。您在检查谷歌验证码或数据保存方面有问题吗?先生,单击“提交”后,我的值存储在DB表中。问题是我是否解决验证码问题。表单始终存储值,所以我只想使验证码成为必需。因此,客户端必须解决验证码以存储表单填写的值,除非他们遇到类似“请解决验证码”的错误。你好,ankit我更新答案你可以使用你的密钥和站点密钥进行检查。先生,我之前已经检查过,但没有显示将表单值存储在db表中的方法。如果成功,你必须存储值。您在检查谷歌验证码或数据保存方面有问题吗?先生,单击“提交”后,我的值存储在DB表中。问题是我是否解决验证码问题。表单始终存储值,所以我只想使验证码成为必需。因此,客户端必须解决验证码以存储表单填写的值,除非他们得到类似“请解决验证码”的错误。你好,ankit我更新答案你可以用你的密钥和站点密钥检查它。