加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 关于我的Google Recaptcha代码

发布时间:2020-12-13 22:27:58 所属栏目:PHP教程 来源:网络整理
导读:我是这个谷歌Recaptcha的新手. 我想弄清楚哪些不起作用.我已经尝试了其他几个代码并且它们不能正常工作,我不明白为什么. 这是我的表单代码 form name="contact_form" method="post" action="functions.php" input type="text" id="fname" name="full_name" p
我是这个谷歌Recaptcha的新手.

我想弄清楚哪些不起作用.我已经尝试了其他几个代码并且它们不能正常工作,我不明白为什么.

这是我的表单代码

<form name="contact_form" method="post" action="functions.php">
 <input type="text" id="fname" name="full_name" placeholder="Full Name" required />
 <input type="number" id="pnumber" name="phone_number" placeholder="Phone Number" required />
 <input type="email" id="emailid" name="email_address" placeholder="Email Address" required />
 <textarea placeholder="Message" name="message" required></textarea>
 <span><input type="checkbox" id="disclaimerid" class="disclaimerclass" name="agreement" value="Agree" required>I have read and agreed with the <a class="dataPrivacyLink" href="#">(Data Protection and Privacy Policy)</a> of Hello World</span>
 <div class="g-recaptcha" data-sitekey="--PUBLIC KEY--"></div>
 <input type="submit" name="sendmessage" class="send-message" value="SUBMIT NOW" />
</form>

这是我的PHP代码

<?php
  function post_captcha($user_response) {
       $fields_string = '';
       $fields = array(
        'secret' => '_______________PRIVATE_KEY_______________','response' => $user_response
    );
    foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string,'&');
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch,CURLOPT_POST,count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result,true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
    // What happens when the CAPTCHA wasn't checked
    echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
    // If CAPTCHA is successfully completed...

    // Paste mail function or whatever else you want to happen here!
    $send;
if( isset( $_POST['email_address'] ) )
{
    $send="Your inquiry submitted successfully. we will contact you very soon. ";
    send_message_to_agent( $_POST['full_name'],$_POST['phone_number'],$_POST['email_address'],$_POST['message'] );
}
else
{
    $send="fail";   
}
function send_message_to_agent( $full_name,$phone_number,$email_address,$visiter_message ){

    $to      = 'hello@world.com';
    $subject = 'Hello World';

    $message = '<p>Name : '.$full_name.'</p>';
    $message .= '<p>Phone Number : '.$phone_number.'</p>';
    $message .= '<p>Email Address : '.$email_address.'</p>';
    $message .= '<p>Message : '.$visiter_message.'</p>';

    $headers = "MIME-Version: 1.0rn";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1rn";

    mail($to,$subject,$message,$headers);
}
echo $send;
//die;
}
// Your code here to handle a successful verification
/>

解决方法

你还必须通过你的ipaddress

$fields = array(‘secret’=>’_______________PRIVATE_KEY_______________’,‘response’=> $USER_RESPONSE,‘remoteip’=> xxx.xxx.xxx.xxx //你的ip);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读