Twe*_*eak 2 php wordpress verification captcha
我正在尝试在我的网站(用PHP和Wordpress开发)中植入Google的reCaptcha V2。
我正在尝试验证用户是否在提交之前检查了此验证码。
这是我的验证:
<?php
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$privatekey = $secret;
$captcha = $_POST['g-recaptcha-response'];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => $privatekey,
'response' => $captcha,
'remoteip' => $_SERVER['REMOTE_ADDR']
);
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);
$ch = curl_init();
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
curl_close($ch);
$jsonResponse = json_decode($response);
if ($jsonResponse->success == true) {
$succMsg = 'Your contact request have submitted successfully.';
echo "<script>alert(\"OK\")</script>";
}
else {
$errMsg = 'Robot verification failed, please try again.';
echo "<script>alert(\"KO ROBOT\")</script>";
}
}
else{
$errMsg = 'Please click on the reCAPTCHA box.';
echo "<script>alert(\"KO CLICK ON BOX\")</script>";
}
?>
Run Code Online (Sandbox Code Playgroud)
当我重新加载页面时,或者当我提交时没有选中验证码时,或者当我选中验证码时,它始终显示:"KO ROBOT"
我也尝试过使用"file_get_contents"而不是curl,但是我遇到了SSL错误警告。
谢谢。
更新:
当我这样做时:
var_dump($jsonResponse);
Run Code Online (Sandbox Code Playgroud)
我在页面上有这个:
object(stdClass)#4028(2){[“”成功“] => bool(false)[”错误代码“] => array(1){[0] =>字符串(20)” invalid-input-secret “}}
更新2:
验证我的私钥后,我现在有了这个:
object(stdClass)#4028(2){[“”成功“] => bool(false)[”错误代码“] => array(1){[0] =>字符串(20)“超时或重复“}}
尝试使用此密钥,只需替换密钥即可。
<?php
$response = isset($_POST["g-recaptcha-response"]) ? $_POST['g-recaptcha-response'] : null;
$privatekey = "YOUR PRIVATE KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'secret' => $privatekey,
'response' => $response,
'remoteip' => $_SERVER['REMOTE_ADDR']
));
$resp = json_decode(curl_exec($ch));
curl_close($ch);
if ($resp->success) {
} else {
//failed return mess
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2487 次 |
| 最近记录: |