简体   繁体   English

新的 Google Recaptcha - PHP 未执行

[英]New Google Recaptcha - PHP not executing

I'm a PHP noob and I am trying to incorporate the new google recaptcha into a site.我是一个 PHP 菜鸟,我正在尝试将新的 google recaptcha 合并到一个站点中。 There is a form in the html doc that calls on a "sendemail.php" file (code below). html 文档中有一个表单调用“sendemail.php”文件(下面的代码)。 The problem is that the form is working, regardless of whether or not the recaptcha done properly by the user.问题是表单正在工作,无论用户是否正确完成了重新验证。 In my code, it will always execute "codeB", regardless of users' failure to complete the recaptcha.在我的代码中,无论用户是否无法完成重新验证,它都会始终执行“codeB”。

What am I screwing up?我在搞什么鬼?

<?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
}
?>

One thing to perhaps check is if the fopen values are turned on in your php.ini.可能要检查的一件事是 fopen 值是否在您的 php.ini 中打开。 I have been having all sorts of problems incorporating the recaptcha into this one particular site.我在将 recaptcha 合并到这个特定站点时遇到了各种各样的问题。 It worked fine on others, and I couldn't figure out why.它在其他人身上工作得很好,我不知道为什么。 Then I saw mention of 'fopen' in another post on this site, and low and behold, the setting was off.然后我在本网站的另一篇文章中看到提到了“fopen”,然后瞧,设置已关闭。

Look for:寻找:

allow_url_fopen
allow_url_include

I changed them both from Off to On and then the code worked!我将它们都从关闭更改为开启,然后代码工作了!

This is a from a tutorial:-这是一个教程:-

 $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>';
    }

hope this helps..希望这可以帮助..

You need to use function json_encode:您需要使用函数 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';
}

There's 2 mistakes with your code, first one is which someone above mentioned that you need to use json_decode.您的代码有 2 个错误,第一个是上面提到的您需要使用 json_decode。 Second of all, you cannot check the value by adding .success infront of $response.其次,您不能通过在 $response 前面添加 .success 来检查值。

Here is the correct code which works:这是有效的正确代码:

$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';
}

In the PHP .INI file on Plesk, there are two lines of code that enable these settings在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM