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?

1
  • A blank page is a PHP error page with disabled error reporting. Enable it for development.
    – Anibal
    Commented Mar 26, 2016 at 13:28

2 Answers 2

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.

For Joomla 3.5 and above use the response from Олег Косарев.

see https://www.1joomla.org/2016/11/display-recaptcha-in-your-custom.html

1

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];
}

Or replace the three lines above with a shorthand line because $recaptcha is not defined if $joomla_captcha is 0.

echo (isset($recaptcha[0])) ? $recaptcha[0] : '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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