[英]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.