关于我的 Google Recaptcha 代码
Posted
技术标签:
【中文标题】关于我的 Google Recaptcha 代码【英文标题】:Regarding my code for Google Recaptcha 【发布时间】:2017-11-17 12:34:00 【问题描述】:我是这个 google 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.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
echo $send;
//die;
// Your code here to handle a successful verification
/>
【问题讨论】:
你用你自己的私钥替换了这一行吗? " '秘密' => '_______________PRIVATE_KEY_______________'," 是的,我已经用我自己的私钥替换了这一行,我只是把它拿出来,因为我在这里粘贴代码:) 【参考方案1】:您还必须传递您的 IP 地址
$fields = array('secret' => '_______________PRIVATE_KEY_______________',
'response' => $user_response,
'remoteip' => xxx.xxx.xxx.xxx //your ip
);
【讨论】:
@HELLOHK 你能支持我的回答吗,有人给了负面评价 它尝试了几次然后停止工作。我不知道为什么。 你必须动态获取你的IP地址。 我该怎么做? 点击此链接将有助于获取 ipaddress Ip Address【参考方案2】:首先您需要包含 reCAPTCHA API javascript 库。
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
在你的表单中
<div class="g-recaptcha" data-sitekey="--PUBLIC KEY--"></div>
在您的帖子功能中
$secret = 'SCREAT_KEY';
//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);
if($responseData->success)
//perform action on success
else
//redirect to login
【讨论】:
在我添加发送操作后,它仍然不起作用。【参考方案3】:这很奇怪,因为在添加我的服务器 ip 后它可以工作一段时间然后它不想再次发送。
【讨论】:
以上是关于关于我的 Google Recaptcha 代码的主要内容,如果未能解决你的问题,请参考以下文章
Google reCaptcha 响应“未捕获(承诺)为空”
如何让 Google reCaptcha 为 Flutter web 工作?
Google Recaptcha & Gatsby Error: reCAPTCHA placeholder element must be an element or id