4

1) Get recaptcha keys from http://www.google.com/recaptcha

2) Set these keys to recaptcha plugin and activate it if it's not.

3) Put below code where you want to show recaptcha

php code:

JPluginHelper::importPlugin('captcha');
$dispatcher = JDispatcher::getInstance();
$dispatcher->trigger('onInit','dynamic_recaptcha_1');

html code inside form tag

<div id="dynamic_recaptcha_1"></div>

I putted this code in my htmp page:

$post = JFactory::getApplication()->input->post;
$dispatcher = JEventDispatcher::getInstance();
$res=$dispatcher->trigger('onCheckAnswer',$post['recaptcha_response_field']);
if(!$res[0]){
    die('Invalid Captcha');
}

I placed the above code but after submitting the form I receive a blank page. Can you explain why?

  • A blank page is a PHP error page with disabled error reporting. Enable it for development. – Anibal Mar 26 '16 at 13:28
4

First read carefully in which files the SO answer that you quoted tells you to put the code.

Also, the code you attached is no longer working in Joomla 3.5. This is due to a change in the reCaptcha plugin. You have to use it like so:

JPluginHelper::importPlugin('captcha');
$dispatcher = JEventDispatcher::getInstance();
$dispatcher->trigger('onInit','my_captcha_div');

And instead of creating a container like this:

<div id="my_captcha_div"></div>

create one like the one below:

<div id="my_captcha_div" 
    class="g-recaptcha" 
    data-sitekey="xxxxxxxxxxxxxx" 
    data-theme="xxxx" 
    data-size="xxxx"></div>

Check here also for more information.

0

Joomla 3.x Correct

    $joomla_captcha = JFactory::getConfig()->get('captcha');
if ( $joomla_captcha != '0') {
    JPluginHelper::importPlugin('captcha');
    $dispatcher = JDispatcher::getInstance();
    // This will put the code to load reCAPTCHA's JavaScript file into your <head>
    $dispatcher->trigger('onInit', 'dynamic_recaptcha_1');
    // This will return the array of HTML code.
    $recaptcha = $dispatcher->trigger('onDisplay', array(null, 'dynamic_recaptcha_1', 'class="required"'));
}

and

if (isset($recaptcha[0]) and $joomla_captcha != "0") {
        echo $recaptcha[0];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.