代码之家  ›  专栏  ›  技术社区  ›  Lee Mac

使用FormMail重述TCHA V2。cgi(Matt的脚本存档)

  •  2
  • Lee Mac  · 技术社区  · 7 年前

    我之前将reCAPTCHA V1与FormMail结合使用。来自Matt脚本归档的cgi,具有以下Perl函数来验证reCAPTCHA响应:

    sub check_captcha {
    
        my $ua = LWP::UserAgent->new();
        my $result=$ua->post(
            'http://www.google.com/recaptcha/api/verify',
            {
                privatekey => 'MyPrivateKey',
                remoteip   => $ENV{'REMOTE_ADDR'},
                challenge  => $Form{'recaptcha_challenge_field'},
                response   => $Form{'recaptcha_response_field'}
            }
        );
        if ( $result->is_success && $result->content =~ /^true/) {
            return;
        } else {
            &error('captcha_failed');
        }
    }
    

    reCAPTCHA V1将于2018年3月底关闭,因此我需要转移到 reCAPTCHA V2 但是,我在验证CGI脚本中的响应时遇到了问题。

    基于 server side documentation ,以下是我迄今为止所做的尝试(未成功):

    sub check_captcha {
    
        my $ua = LWP::UserAgent->new();
        my $result=$ua->post(
            'https://www.google.com/recaptcha/api/siteverify',
            {
                secret     => 'MyPrivateKey',
                remoteip   => $ENV{'REMOTE_ADDR'},
                response   => $Form{'g-recaptcha-response'}
            }
        );
        if ( $result->is_success && $result->content =~ /"success": true/ ) {
            return;
        } else {
            &error('captcha_failed');
        }
    }
    

    以上内容始终会转移到“captcha\u failed”(验证码失败)错误。

    提前感谢您抽出时间阅读我的问题,我感谢社区提供的任何帮助。

    非常感谢!

    1 回复  |  直到 7 年前
        1
  •  2
  •   Dave Cross    7 年前

    我看不出你的代码有任何明显的问题。但我想知道你为什么要自己实施这个 Google::reCAPTCHA 存在。

    use Google::reCAPTCHA;
    
    my $c = Google::reCAPTCHA->new( secret => 'MyPrivateKey' );
    
    # Verifying the user's response 
    my $success = $c->siteverify(
      response => $Form{'g-recaptcha-response'},
      remoteip => $ENV{'REMOTE_ADDR'},
    );
    
    if ( $success ) {
      # CAPTCHA was valid
    }
    

    为什么? 您是否使用Matt脚本存档中的代码?

    推荐文章