2017-01-10 26 views
0

我試圖安裝谷歌驗證碼爲聯繫人頁面,我已經實在有限用PHP知識安裝信息對於谷歌驗證碼。我不確定Google要求的信息應該放在我的php文件中。以下是谷歌的說明爲:的聯繫表格PHP文件

當您的用戶請在您集成驗證碼的形式,你會得到的有效載荷名爲「G-reCAPTCHA的響應」的字符串的一部分。爲了檢查是否谷歌已經驗證的用戶,發送這些參數的POST請求:

網址:https://www.google.com/recaptcha/api/siteverify

祕密(必需) - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

響應(必需) - 的「G值-recaptcha響應」。

REMOTEIP - 最終用戶的IP地址。

這裏是我的,因爲我使用的形式PHP。

<?php 
$secret = 'SECRET KEY HERE'; 
$verificationResponse = $_POST["g-recaptcha-response"]; 

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$verificationResponse); 
$response = json_decode($response, true); 
if($response["success"] === true){ 
// actions if successful 
}else{ 
// actions if failed 
} 

/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['inputName'], "First and Last"); 
$email = check_input($_POST['inputEmail'], "Required"); 
$phone = check_input($_POST['inputPhone']); 
$message = check_input($_POST['inputMessage'], "Brief Description"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Invalid e-mail address"); 
} 
/* Let's prepare the message for the e-mail */ 

$subject = "Contact Message from thewiseinvestor.net"; 

$message = " 

Someone has sent you a message using your contact form: 

Name: $name 
Email: $email 
Phone: $phone 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location:contact.html'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
exit(); 
} 
?> 

HTML表單

<div class="row"> 
    <div class="col-md-6 message"> 
    <h2>Send Us A Message</h2> 
    <form name="contactform" method="post" action="index.php" class="form-vertical"> 
     <div class="form-group"> 
     <label for="inputName" class="control-label">Name</label> 
      <input type="text" class="form-control" id="inputName" name="inputName" placeholder="First and Last"> 
     </div> 
     <div class="form-group"> 
     <label for="inputEmail" class="control-label">Email*</label> 
      <input type="text" class="form-control" id="inputEmail" name="inputEmail" placeholder="Required"> 
     </div> 
     <div class="form-group"> 
     <label for="inputPhone" class="control-label">Phone Number</label> 
      <input type="text" class="form-control" id="inputPhone" name="inputPhone" placeholder="Optional"> 
     </div> 
     <div class="form-group"> 
     <label for="inputMessage" class="control-label">Message</label> 
      <textarea class="form-control" rows="5" id="inputMessage" name="inputMessage" placeholder="Brief Description"></textarea> 
     </div> 
     <div class="g-recaptcha" data-sitekey="DATA SITE KEY HERE"></div> 
     <div class="form-group"> 
     <button type="submit" class="btn btn-custom pull-right hvr-underline-from-left">Send</button> 
     </div> 
    </form> 
    </div> <!-- end col-md-6 --> 

我到哪裏了上述信息應該去真的不確定。任何援助非常感謝。

回答

1

在谷歌驗證碼機制注入你的表單中隱藏的iframe,並將散列字符串返回給您的處理腳本,稱爲「g-recaptcha-response」。

所以,在你上面的PHP腳本,/* Set e-mail recipient */之前,請添加以下內容:

<?php 

// error_reporting(E_WARNING); 

function readURL($url) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch); 
    return $output; 
} 

$secret = "PASTE-YOUR-SECRET-KEY-HERE"; 
$verificationResponse = $_POST["g-recaptcha-response"]; 
if(empty($verificationResponse)) die("Google did not POST the required g-recaptha-response"); 

$response = readURL("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $verificationResponse . ""); 

$responseArray = json_decode($response, true); 
if($responseArray["success"] !== true) die("Invalid reCaptcha <a href=\"javascript:history.go(-1);\">Try Again</a>"); 

/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* Check all form inputs using check_input function */ 
$name = check_input($_POST['inputName'], "First and Last"); 
$email = check_input($_POST['inputEmail'], "Required"); 
$phone = check_input($_POST['inputPhone']); 
$message = check_input($_POST['inputMessage'], "Brief Description"); 

/* If e-mail is not valid show error message */ 
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) 
{ 
show_error("Invalid e-mail address"); 
} 
/* Let's prepare the message for the e-mail */ 

$subject = "Contact Message from thewiseinvestor.net"; 

$message = " 

Someone has sent you a message using your contact form: 

Name: $name 
Email: $email 
Phone: $phone 

Message: 
$message 

"; 

/* Send the message using mail() function */ 
mail($myemail, $subject, $message); 

/* Redirect visitor to the thank you page */ 
header('Location:contact.html'); 
exit(); 

/* Functions we used */ 
function check_input($data, $problem='') 
{ 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
if ($problem && strlen($data) == 0) 
{ 
show_error($problem); 
} 
return $data; 
} 

function show_error($myError) 
{ 
?> 
<html> 
<body> 

<p>Please correct the following error:</p> 
<strong><?php echo $myError; ?></strong> 
<p>Hit the back button and try again</p> 

</body> 
</html> 
<?php 
exit(); 
} 

?> 

應該沒有任何問題的工作。該代碼將檢查驗證碼是檢查其他的東西或向您發送的任何電子郵件之前正確地傳遞。

祝你好運。

+0

謝謝@Ruslan Abuzant。這確實有幫助。我將在你陳述的地方添加以下內容,對於另一個愚蠢的問題抱歉,所需的谷歌密鑰,上面的內容將在哪裏出現? – user3786102

+0

不用擔心,只需編輯剪切的代碼併爲您提供一個簡單地複製粘貼密鑰的位置。正如你所看到的那樣,它以'$ secret'的形式傳遞給google驗證URL。 –

+0

我快到了@Ruslan Abuzant。我更新了我的php以反映你所建議的改變。我也粘貼了我的html表單。當我點擊發送時,我收到了與第6行有關的3個錯誤。**警告:通過/nfs/c12/h08/mnt/215915/domains/thewiseinvestor.net/html/index.php第6行中的allow_url_fopen = 0,在服務器配置中禁用了file_get_contents():https:// wrapper * * – user3786102

0

沒有爲reCAPTCHA的官方文檔,並準備使用PHP庫。

這裏你可以找到準備使用代碼和註釋: https://github.com/google/recaptcha

您的服務器端代碼會是這樣的:

<?php 
$recaptcha = new \ReCaptcha\ReCaptcha($secret); 
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); 
if ($resp->isSuccess()) { 
    // verified! 
} else { 
    $errors = $resp->getErrorCodes(); 
} 
+1

用戶聲明他們對php有限的知識。我懷疑引入命名空間類和第三方庫將有任何幫助。不過,您的答案當然是有用的,謝謝。 ;) –