[英]New Google Recaptcha - PHP not executing
我是一个 PHP 菜鸟,我正在尝试将新的 google recaptcha 合并到一个站点中。 html 文档中有一个表单调用“sendemail.php”文件(下面的代码)。 问题是表单正在工作,无论用户是否正确完成了重新验证。 在我的代码中,无论用户是否无法完成重新验证,它都会始终执行“codeB”。
我在搞什么鬼?
<?php
$captcha=$_POST['g-recaptcha-response'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=##########11&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
//codeA that I want to execute if the recaptcha fails
echo '<p>Please Go Back And Try Again</p>';
}
else
{
//codeB that I want to execute upon success of the recaptcha
}
?>
可能要检查的一件事是 fopen 值是否在您的 php.ini 中打开。 我在将 recaptcha 合并到这个特定站点时遇到了各种各样的问题。 它在其他人身上工作得很好,我不知道为什么。 然后我在本网站的另一篇文章中看到提到了“fopen”,然后瞧,设置已关闭。
寻找:
allow_url_fopen
allow_url_include
我将它们都从关闭更改为开启,然后代码工作了!
这是一个教程:-
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['comment'])){
$email=$_POST['comment'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}else
{
echo '<h2>Thanks for posting comment.</h2>';
}
希望这可以帮助..
您需要使用函数 json_encode:
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$response = json_encode($response);
if($response->success==false){
echo 'spamer';
}
else{
echo 'ok';
}
您的代码有 2 个错误,第一个是上面提到的您需要使用 json_decode。 其次,您不能通过在 $response 前面添加 .success 来检查值。
这是有效的正确代码:
$captchaSecretCode = 'placeYourSecretCodeHere';
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$captchaSecretCode."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == true)
{
echo 'true';
}else{
echo 'false';
}
在 Plesk 上的 PHP .INI 文件中,有两行代码可以启用这些设置
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;
; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = On
; Whether to allow include/require to open URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-include
allow_url_include = On
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.