2017-01-10 2 views
0

私はコンタクトページのGoogle reCaptchaをインストールしようとしています。私はGoogleの情報が私のPHPファイルに入る必要がどこに不明です。インストール情報Contact FormのPHP reCaptcha PHPファイル

ユーザーがreCAPTCHAを統合したフォームを送信すると、ペイロードの一部として「g-recaptcha-response」という名前の文字列が取得されます。 Googleのかどうかをチェックするためには、ユーザは、これらのパラメータを持つPOSTリクエストを送信していることを確認しました:

URL:https://www.google.com/recaptcha/api/siteverify

秘密(必須) - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

応答(必須) - G」の値を-recaptcha-response 'を指定します。

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

GoogleのreCAPTCHAのメカニズムは、フォーム内の隠し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(); 
} 

?> 

は何の問題もなく動作するはずです。コードは、他のものをチェックしたり電子メールを送る前にreCaptchaが正しく渡されたかどうかをチェックします。

幸運。

+0

@Ruslan Abuzantありがとうございました。これは本当に役に立ちます。私はあなたが述べたところで、別のダムの質問、申し訳ありません、必要なGoogleの秘密鍵を追加するつもりです、上記のどこに行くだろうか? – user3786102

+0

心配しなくても、コードを編集して、単にあなたのキーをコピー・ペーストする場所を提供しました。ご覧のとおり、Google認証のURLに '$ secret'として渡されます。 –

+0

私はほとんどそこにいる@Ruslan Abuzant。あなたが提案した変更を反映するために上記のPHPを更新しました。私はまた、私のHTMLフォームに貼り付けました。私はsendを押すと6行目に関連した3つのエラーが出ます。**警告:file_get_contents():サーバ設定で、/nfs/c12/h08/mnt/215915/domains/thewiseinvestor.net/html/index.php on line 6のallow_url_fopen = 0によってhttps://ラッパーが無効になっています。 * – user3786102

0

reCaptchaの公式ドキュメントがあり、PHP libを使用する準備ができています。

ここでは、コードとコメントを使用する準備ができて見つける:あなたのサーバー側のコードは次のようになります 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で知識が限られている」と述べています。私はネームスペースのクラスを導入することは疑いありませんし、サードパーティのライブラリは助けになるでしょう。それでも、あなたの答えはもちろん役に立ちます、ありがとうございます。 ;) –