我以前使用reCAPTCHA V1和Matt脚本存档的FormMail.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_failed'错误。
提前感谢您抽出时间阅读我的问题,感谢社区提供的任何帮助。
非常感谢!
我看不到你的代码有任何明显的问题。但是我想知道为什么当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
}
你为什么要使用马特脚本档案中的代码?