2017-02-11 9 views
16

मैं अपने jquery बटन को उपयोगकर्ताओं को परेशान किए बिना बॉट से सुरक्षित रखना चाहता हूं, इसलिए मैंने Google के अदृश्य रीपैप्चा को जोड़ने का विचार किया। हालांकि कार्यान्वयन उतना आसान नहीं है जितना मैं और मैं ऐसा नहीं कर सकता। अगर कोई मुझे दिखा सकता है कि यह कैसे किया जाता है तो यह बहुत अच्छा होगा। पीएस: मैं इसे एक WordPress विषय पर कर रहा हूँ।अदृश्य रीकैप्चा के साथ jquery बटन की रक्षा कैसे करें?

https://developers.google.com/recaptcha/docs/invisible

अदृश्य recaptcha बनाएँ::

यह प्रलेखन है

https://www.google.com/recaptcha/admin#beta

और यह मैं क्या है:

HTML:

<button class="acf-get-content-button">Show Link</button> 
<div class="fa" id="acf-content-wrapper" data-id="<?php echo $post_id; ?>"></div> 

जे एस:

<script> 
(function($) { 
    $('.acf-get-content-button').click(function(e) { 
    e.preventDefault(); 
    $('.fa').addClass('fa-cog fa-spin fa-4x'); 
    var $contentWrapper = $('#acf-content-wrapper'); 
    var postId = $contentWrapper.data('id'); 

    $.ajax({ 
     url: "/public/ajax.php", 
     type: "POST", 
     data: { 
      'post_id': postId 
     }, 
     }) 
     .done(function(data) { 
     $('.fa').removeClass('fa-cog fa-spin fa-4x'); 
     $contentWrapper.append(data); 
     $('.acf-get-content-button').removeClass().addClass('.acf-get-content-button') 
     }); 
    }); 
    $('.acf-get-content-button').mouseup(function() { 
    if (event.which == 1) { 
     $(".acf-get-content-button").hide(); 
    } 
    }); 
})(jQuery); 
</script> 

ajax.php

<?php 
define('WP_USE_THEMES', false); 
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); 
global $post; 
$post_id = $_REQUEST["post_id"]; 
$content = get_field('ebook_link_pdf', $post_id); 
echo ($content); 
+0

आपको वर्डप्रेस में AJAX के साथ nonce का उपयोग करना चाहिए, जो निश्चित रूप से बॉट्स में मदद करेगा। इनपुट के लिए –

उत्तर

6

आप Invisible reCaptcha for WordPress प्लगइन का उपयोग यह आसानी से कर सकते हैं अगर आपको लगता है खरोंच से कोडिंग आप के लिए जटिल है। कार्यान्वयन के बारे में एक विचार प्राप्त करने के लिए आप प्लगइन के स्रोत कोड में भी खुदाई कर सकते हैं।

इस प्लगइन में कस्टम उपयोग के लिए क्रियाएं और फ़िल्टर हैं और इन्हें प्लगइन होमपेज पर दस्तावेज किया गया है।

+0

हाय धन्यवाद। मैं इस तथ्य को कैसे संभाल सकता हूं कि मेरा एक बटन है और एक रूप नहीं है? –

3

मैं reCaptcha के साथ प्रयोग करने के लिए आगे बढ़ गया।

API के अनुसार बाहर निकलता है, तो आप अपने AJAX कॉल को सबमिट करने के लिए grecaptcha.getResponse विधि का उपयोग कर सकते हैं। (लेकिन ध्यान दें कि यह ReCAPTCHA के एपीआई परीक्षण में अब भी है और बदल सकता है ...) यहाँ एक छोटी उदाहरण है:

HTML:

<div id="test-captcha" class="g-recaptcha" data-sitekey=[Your site key]></div> 
<button id="load" onclick="go();">Load something</button> 

जावास्क्रिप्ट:

function go() 
{ 
    $.ajax({ 
     url: "/captchatest.php", 
     type: "POST", 
     data: { 
      'g-recaptcha-response': grecaptcha.getResponse() 
     } 
    }).done(function(data) { 
     alert(data); 
    }); 
} 

captchatest.php

<?php 
//Used http://stackoverflow.com/a/6609181/7344257 
function do_post_request($url, $data) 
{ 
    // use key 'http' even if you send the request to https://... 
    $options = array(
      'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
        'method' => 'POST', 
        'content' => http_build_query($data) 
      ) 
    ); 
    $context = stream_context_create($options); 
    $result = file_get_contents($url, false, $context); 
    if ($result === FALSE) { /* Handle error */ } 
    return $result; 
} 

$error = ""; 
if ($_SERVER["REQUEST_METHOD"] === "POST") 
{ 
    if (!isset($_POST['g-recaptcha-response'])) 
    { 
     echo "Please do reCaptcha"; 
     exit(0); 
    } 

    $data = array("secret" => "6LeUGhYUAAAAABNS5OtOc9vonTlyrtgcQ5VdI7cV", 
       "response" => $_POST['g-recaptcha-response'], 
       "remoteip" => $_SERVER["REMOTE_ADDR"] //This is optional. 
    ); 
    $resp = json_decode(do_post_request("https://www.google.com/recaptcha/api/siteverify", $data)); 
    if (!$resp->success) 
    { 
     //use $resp->error-codes to debug error. 
     echo "Invalid reCaptcha"; 
     exit(0); 
    } 
    echo "Received secret code."; 
    exit(0); 
} 
?> 

मुझे यकीन नहीं था कि क्या आप curl का उपयोग कर सकते हैं। तो मैंने बस बुनियादी PHP कोड के साथ चिपकने का फैसला किया। आपको त्रुटियों को प्रारूपित करना होगा, लेकिन मुझे लगता है कि आपको बिंदु प्राप्त करना चाहिए।

संबंधित मुद्दे