2015-05-02 8 views
41

के साथ मैंने अपनी वेबसाइट में रीकैप्चा 2.0 काम करने में कामयाब रहा है। हालांकि, यह केवल तभी काम कर रहा है जब मैं AJAX का उपयोग नहीं करता और मैंने फ़ॉर्म को "स्वाभाविक रूप से" सबमिट करने दिया।पुन: कैप्चाचा 2.0 AJAX

मैं कैप्चा के साथ फॉर्म जमा करना चाहता हूं और उपयोगकर्ता को सफलता नोट पृष्ठ को रीफ्रेश किए बिना चेतावनी देना चाहता हूं।

मैं निम्नलिखित कोड की कोशिश की, लेकिन जैसे सर्वर उपयोगकर्ता प्रतिक्रिया नहीं करता है ऐसा लगता है:

HTML:

<form class="form" action="javascript:void(0)" novalidate> 
    <!-- all the inputs... --> 

    <!-- captcha --> 
    <div class="input-group"> 
     <div class="g-recaptcha" data-sitekey="6LdOPgYTAAAAAE3ltWQGar80KUavaR-JblgPZjDI"></div> 
    </div> 

    <div class="errors" id="errors" style="display: none"></div> 

    <div class="input-group"> 
     <input type="button" value="Send" class="btn-default right" id="submit"> 
     <div class="clear"></div> 
    </div> 
</form> 

जे एस:

$('#submit').click(function(e) { 
    console.log('clicked submit'); // --> works 

    var $errors = $('#errors'), 
     $status = $('#status'), 

     name = $('#name').val().replace(/<|>/g, ""), // prevent xss 
     email = $('#email').val().replace(/<|>/g, ""), 
     msg = $('#message').val().replace(/<|>/g, ""); 

    if (name == '' || email == '' || msg == '') { 
     valid = false; 
     errors = "All fields are required."; 
    } 

    // pretty sure the problem is here 
    console.log('captcha response: ' + grecaptcha.getResponse()); // --> captcha response: 

    if (!errors) { 
     // hide the errors 
     $errors.slideUp(); 
     // ajax to the php file to send the mail 
     $.ajax({ 
      type: "POST", 
      url: "http://orenurbach.com/assets/sendmail.php", 
      data: "email=" + email + "&name=" + name + "&msg=" + msg + "&g-recaptcha-response=" + grecaptcha.getResponse() 
     }).done(function(status) { 
      if (status == "ok") { 
       // slide down the "ok" message to the user 
       $status.text('Thanks! Your message has been sent, and I will contact you soon.'); 
       $status.slideDown(); 
       // clear the form fields 
       $('#name').val(''); 
       $('#email').val(''); 
       $('#message').val(''); 
      } 
     }); 
    } else { 
     $errors.text(errors); 
     $errors.slideDown(); 
    } 
}); 

PHP:

<?php 
    // assemble the message from the POST fields 

    // getting the captcha 
    $captcha = ''; 
    if (isset($_POST['g-recaptcha-response'])) 
     $captcha = $_POST['g-recaptcha-response']; 
    echo 'captcha: '.$captcha; 

    if (!$captcha) 
     echo 'The captcha has not been checked.'; 
    // handling the captcha and checking if it's ok 
    $secret = 'MY_SECRET'; 
    $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true); 

    var_dump($response); 

    // if the captcha is cleared with google, send the mail and echo ok. 
    if ($response['success'] != false) { 
     // send the actual mail 
     @mail($email_to, $subject, $finalMsg); 

     // the echo goes back to the ajax, so the user can know if everything is ok 
     echo 'ok'; 
    } else { 
     echo 'not ok'; 
    } 
?> 

परिणाम पीएचपी पेज में:

captcha: The captcha has not been checked.array(2) { ["success"]=> bool(false) ["error-codes"]=> array(1) { [0]=> string(22) "missing-input-response" } } not ok 

निष्कर्ष है, मैं कैसे मैन्युअल रूप से बिना यह स्वचालित रूप से पोस्ट डेटा के साथ जा रहा इनपुट प्रतिक्रिया मिल सकता है?

+0

कंसोल में 'grecaptcha.getResponse()' के लिए आपको क्या मिलता है? खाली? – Samurai

+0

@ सैमुराई यूप। यही कारण है कि मुझे लगता है कि समस्या है। – Gofilord

+1

ठीक है जब तक यह सत्यापित नहीं होता है कि यह खाली हो जाता है (उत्तर नहीं दिया गया है या गलत तरीके से जवाब दिया गया है)। लेकिन सत्यापन के बाद आपको काफी लंबी स्ट्रिंग मिलनी चाहिए। – Samurai

उत्तर

23

ठीक है, यह बहुत मूर्ख था।

मैं चीजों की एक जोड़ी गलत किया है:

  • PHP फ़ाइल में, तार उन पर एकल उद्धरण था, और कहा कि समस्याओं के कारण।
  • परीक्षण के दौरान, मैंने PHP फ़ाइल में चीजों की कई प्रिंटिंग जोड़े, इस प्रकार if (status == "ok") कभी काम नहीं कर रहा था। मुझे ईमेल प्राप्त हुए लेकिन मुझे कोई भी रचना नहीं मिली जो मैंने किया था और अब मैं देखता हूं क्यों।
  • जब मैं यह जांचना चाहता था कि PHP फ़ाइल क्या छोड़ रही थी, तो मैं बस यूआरएल में इसके पते पर गया और हमेशा एक त्रुटि मिली। यहां तक ​​कि जब मेल भेजे गए थे। अब मैं समझता हूं कि लॉग की जांच करने का यह सही तरीका नहीं है।

@ सैमुराई के लिए धन्यवाद जिन्होंने मेरी चीजों को समझने में मदद की।


अंतिम PHP कोड:

<?php 
    // assemble the message from the POST fields 

    // getting the captcha 
    $captcha = ""; 
    if (isset($_POST["g-recaptcha-response"])) 
     $captcha = $_POST["g-recaptcha-response"]; 

    if (!$captcha) 
     echo "not ok"; 
    // handling the captcha and checking if it's ok 
    $secret = "MY_SECRET"; 
    $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"]), true); 

    // if the captcha is cleared with google, send the mail and echo ok. 
    if ($response["success"] != false) { 
     // send the actual mail 
     @mail($email_to, $subject, $finalMsg); 

     // the echo goes back to the ajax, so the user can know if everything is ok 
     echo "ok"; 
    } else { 
     echo "not ok"; 
    } 
?> 
+2

क्या आप अपने उत्तर के पूरक के लिए यहां अंतिम कोड/मार्कअप प्रदान कर सकते हैं? – elmimmo

+0

दोस्त को धन्यवाद देने के लिए बस लॉग इन करें! बीटीडब्ल्यू, पीएचपी कोड में, पहले 'गूंज "के बाद ठीक नहीं है"; 'if' कथन के अंदर आप' बाहर निकलें; 'कमांड डाल देंगे। –

+0

@joao_pimentel क्यों? – Gofilord

0

आप अपने जावास्क्रिप्ट में इन डॉलर चिह्न है चाहते थे? यदि हां, तो कैसे आते हैं?

var $errors = $('#errors'), 
$status = $('#status'), 
+0

यह प्रश्न का उत्तर नहीं प्रदान करता है। आप [समान प्रश्नों की खोज कर सकते हैं] (// stackoverflow.com/search), या उत्तर खोजने के लिए पृष्ठ के दाईं ओर संबंधित और जुड़े प्रश्नों का संदर्भ लें। यदि आपके पास एक संबंधित लेकिन अलग प्रश्न है, [एक नया प्रश्न पूछें] (// stackoverflow.com/questions/ask), और संदर्भ प्रदान करने में सहायता के लिए इस लिंक को शामिल करें। देखें: [प्रश्न पूछें, जवाब प्राप्त करें, कोई विकृति नहीं है] (// stackoverflow.com/tour) –

+0

'$ ('# status') 'जावास्क्रिप्ट लाइब्रेरी के लिए एक चयनकर्ता है [jQuery] (https://jquery.com /)। डॉलर के संकेतों के साथ नामकरण चर भी ठीक है, 'var $ त्रुटियां' पूरी तरह से मान्य है और मूल जावास्क्रिप्ट में काम कर रही है। –

+0

यह प्रश्न का उत्तर नहीं प्रदान करता है। एक बार आपके पास पर्याप्त [प्रतिष्ठा] (https://stackoverflow.com/help/whats-reputation) हो जाने पर आप [किसी भी पोस्ट पर टिप्पणी कर सकेंगे] (https://stackoverflow.com/help/privileges/comment); इसके बजाय, (https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- [जवाब है कि प्रश्नकर्ता से स्पष्टीकरण की आवश्यकता नहीं है प्रदान करते हैं] i-कर-बजाय)। - [समीक्षा से] (/ समीक्षा/कम गुणवत्ता वाली पोस्ट/18877553) –