2017-01-04 215 views
0

新しいGoogle reCaptchaを既存のフォーム処理PHPスクリプトに統合する際に問題があります。以前は、contact.htmlページをcontact.php電子メールハンドラにリダイレクトしても問題はありませんでしたが、私はスパムの負荷を受けていました。したがって、reCaptchaを使用したいという欲求がありました。reCaptchaを既存のcontact.phpに統合

私は電子メールを処理するために別のPHPファイルを使用します。次のように関連するcontact.htmlコードは次のとおりです。

<form id="contact-form" method="post" action="contact.php" role="form"> 

<div class="messages"></div> 
<div class="controls"> 

    <div class="row"> 
     <div class="col-md-7"> 
      <div class="form-group"> 
       <label for="form_name">Name *</label> 
       <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your name *" required="required" data-error="Name is required"> 
       <div class="help-block with-errors"></div> 
      </div> 
      <div class="form-group"> 
       <label for="form_email">Email *</label> 
       <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email address *" required="required" data-error="A valid email is required"> 
       <div class="help-block with-errors"></div> 
     </div> 
     <div class="form-group"> 
       <label for="form_phone">Telephone</label> 
       <input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter a contact telephone number (optional)"> 
       <div class="help-block with-errors"></div> 
      </div> 
     <div class="form-group"> 
       <label for="form_message">Message *</label> 
       <textarea id="form_message" name="message" class="form-control" placeholder="Please enter your message *" rows="4" required="required" data-error="Please enter your message"></textarea> 
       <div class="help-block with-errors"></div> 
      </div> 
     <p> 
      <div class="g-recaptcha" data-sitekey="6LfsexAUAAAAAF_qKlK7De8kA7XM2MGrGKTyK60M"></div></p> 

     <input type="submit" class="btn btn-success btn-send" value="Submit"></p> 
     <br><p class="text-muted"><strong>*</strong> These fields are required.</p> 
     </form> 

contact.phpファイルから既存のコードはこれです:

<?php 


$from= "[email protected]"; 
$sendTo = "[email protected]"; 
$subject = "New message from contact form"; 
$fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); 
$okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.'; 
$errorMessage = 'There was an error while submitting the form. Please try again later'; 


try 
{ 
$emailText = "You have new message from contact form\n=============================\n"; 

foreach ($_POST as $key => $value) { 

    if (isset($fields[$key])) { 
     $emailText .= "$fields[$key]: $value\n"; 
    } 
} 

mail($sendTo, $subject, $emailText, "From: " . $from); 

$responseArray = array('type' => 'success', 'message' => $okMessage); 
} 
catch (\Exception $e) 
{ 
$responseArray = array('type' => 'danger', 'message' => $errorMessage); 
} 

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
$encoded = json_encode($responseArray); 

header('Content-Type: application/json'); 

echo $encoded; 
} 
else { 
echo $responseArray['message']; 
} 

これは現在正常に動作しますが、私はreCAPTCHAの検証を試みると統合するときPHPには、電子メールは生成されず、PHPの成功メッセージはWebページに表示されません。

reCaptcha検証をphpファイルに統合する際の助けがあれば、大変感謝しています!

EDIT:reCaptchaのタグは、必要に応じてHTMLの中にあり、ウィジェットはサイト上に正しく表示され、機能します。しかし、私は既存のPHPに統合しようとしたコードのすべての例は働いていないし、電子メールは生成されません(なぜ私は上記のPHPファイルでそれを残している)。前もって感謝します!

EDIT 2:私はPHPスクリプトを改訂し、CoolCodeGuyの有用なコメントの後にクリーンアップしようとしました。しかし、私の予算のPHPスキルを考えると、今は動作しません。助けてください!!

  <?php 


    $from= "[email protected]"; 
    $sendTo = "[email protected]"; 
    $subject = "New message from contact form"; 
    $fields = array('name' => 'Name', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); 
    $okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.'; 
    $errorMessage = 'There was an error while submitting the form. Please try again later'; 
    $url = 'https://www.google.com/recaptcha/api/siteverify'; 
    $privatekey = "xxxxxxxxx"; //whatever your PRIVATE key is 
    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']); 
    $data = json_decode($response); 

    try 
    { 
    $emailText = "You have new message from contact form\n=============================\n"; 

    foreach ($_POST as $key => $value) { 
    //verifcation passed 
    if (isset($fields[$key])) { 
     $emailText .= "$fields[$key]: $value\n"; 
    } 
} 

    mail($sendTo, $subject, $emailText, "From: " . $from); 
    $responseArray = $okMessage; 
    } 
    else 
    { 
    //verification failed 
    $responseArray = $errorMessage; 
    } 

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
    $encoded = json_encode($responseArray); 

    header('Content-Type: application/json'); 

    echo $encoded; 
    } 
    else { 
    echo $responseArray['message']; 
    } 

答えて

0

私はソーシャルネットワーキングサイトを運営しているように、コード全体を更新しました。

 <?php 

    $sendTo = "[email protected]"; 
    $subject = "New message from contact form"; 
    $headers .= 'From: <[email protected]>' . "\r\n"; 

    $name = @$_POST['name']; 
    $phone = @$_POST['phone']; 
    $email = @$_POST['email']; 
    $message = @$_POST['message']; 

    $okMessage = 'Thank you for your message. One of the team will be in touch as soon as possible.'; 
    $errorMessage = 'There was an error while submitting the form. Please try again later'; 
    $url = 'https://www.google.com/recaptcha/api/siteverify'; 
    $privatekey = "xxxxxxxxx"; //whatever your PRIVATE key is 
    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']); 
    $data = json_decode($response); 

    $emailText = "Name: $name \n Phone: $phone \n Email: $email \n Message: $message"; 

if (isset($data->success) AND $data->success==true) { 

    mail($sendTo, $subject, $emailText, $headers); 
    $responseArray = $okMessage; 
    } 
    else 
    { 
    //verification failed 
    $responseArray = $errorMessage; 
    } 

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
    $encoded = json_encode($responseArray); 

    header('Content-Type: application/json'); 

    echo $encoded; 
    } 
    else { 
    echo $responseArray; 
    } 
+0

ありがとうございます。電子メールは今、(あなたがreCaptchaを渡したと仮定して)うまく生成します。ただし、成功/エラーメッセージは、ページ上または新しいページ上で生成されていません。私は、古い成功コード($ responseArray = array( 'type' => 'success'、 'message' => $ okMes​​sage);を組み込み、エコー$ responseArray ['message']を追加しようとしました;しかし、PHPスクリプトのしかしまだ運がない... –

+0

@JoeFlahertyあなたは 'if(for!($ _ SERVER ['HTTP_X_REQUESTED_WITH'])== 'xmlhttprequest'){ $ encoded = json_encode($ responseArray); header( 'Content-Type:application/json'); echo $ encoded; 'するコードの? $とは何ですか? – CoolCodeGuy

+0

@JoeFlahertyそれほど重要でない場合は、else文を停止してエコーコードを停止していると思うので、取り除くことをお勧めします。 – CoolCodeGuy

0

フォームタグに、

$url = 'https://www.google.com/recaptcha/api/siteverify'; 
    $privatekey = ""; //whatever your PRIVATE key is 

    $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']); 

    $data = json_decode($response); 

    if (isset($data->success) AND $data->success==true) { 
    //verification succeeded 
    $responseArray = $okMessage; 
    } 
    else 
    { 
    //verification failed 
    $responseArray = $errorMessage; 
    } 

echo $responseArray; 
+0

あなた、ありがとうございます。電子メールが正常に生成されるようになりましたが、成功/失敗メッセージが機能しなくなりました。私はオリジナルのコメント(phpはあなたがおそらく言うことができるように私の強い訴訟ではない)の編集でphpを修正しました。助けてください!! –

+0

@JoeFlaherty配列を使用せずに自分のコードに組み込むための更新されたコードを見てください(個人的には複雑すぎると思います!) – CoolCodeGuy

+0

ありがとう。私はそれを組み込もうとしましたが、まだ動作していません - エラー/成功メッセージや電子メールはありません。 okMes​​sageを生成するだけでなく、実際に電子メールスクリプトも実行するようにしたいと考えています。私はオリジナルのコメントに完全な改訂版のスクリプトを貼り付けました。ありがとう。 –

0

は、なぜあなたはjavascriptの検証を使用していけないcontact.phpページに以下のコードを組み込む

<div class="g-recaptcha" id="rcaptcha" data-sitekey="xxxx"></div> 
    `  <form action="" method="post" onSubmit="return get_action()"> ` <span id="captcha" style="color:red" /></span></div> 

<script src='https://www.google.com/recaptcha/api.js'></script> 
    <script> 

    function get_action(form) 
    { 
    var v = grecaptcha.getResponse(); 
if(v.length == 0) 
{ 
    document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty"; 
    return false; 
} 
else 
{ 
    document.getElementById('captcha').innerHTML="Captcha completed"; 
    return true; 
    } 
    } 

+0

難しいと思うかもしれませんが、私はPHPに比べJSのことは分かりません。私は物事を可能な限りシンプルに保つように努めています!! –

関連する問題