Jump to content

Markup Google reCAPTCHA


flydev

Recommended Posts

MarkupGoogleRecaptcha

Google reCAPTCHA for ProcessWire.

This module simply adds reCAPTCHA V2 or Invisible reCAPTCHA to your form.


How To Install

  1. Download the zip file at Github or from the modules repository
  2. Drop the module files in /site/modules/MarkupGoogleRecaptcha
  3. In your admin, click Modules > Refresh
  4. Click "install" for "MarkupGoogleRecaptcha"

Official install/uninstall doc: http://modules.processwire.com/install-uninstall/

 

API

You must create an API key prior to use this module. Goto https://www.google.com/recaptcha/admin to create your own. Next, add the API keys information to the module's settings.

api.thumb.png.b0775a282bf6a51d48816dc347d9ae35.png

 

Usage

  1. Call the module : $captcha = $modules->get("MarkupGoogleRecaptcha");
  2. Call $captcha->getScript(); somewhere to get the javascript used by reCAPTCHA
  3. Render reCAPTCHA in a standard HTML <form></form> by calling $captcha->render() or Render reCAPTCHA in an InputfieldForm by passing as argument your form to the render function: $captcha->render($form)
  4. Call verifyResponse() to get the result. It return TRUE if the challenge was successful.

 

Example

  • Using ProcessWire's form API :
$out = '';

$captcha = $modules->get("MarkupGoogleRecaptcha");
// if submitted, check response
if ($captcha->verifyResponse() === true)
{
    $out .= "Hi " . $input->post["name"].", thanks for submitting the form!";
}
else
{
    $form = $modules->get("InputfieldForm");
    $form->action = $page->url;
    $form->method = "post";
    $form->attr("id+name", "form");

    $field = $this->modules->get('InputfieldText');
    $field->name = "name";
    $field->placeholder = "name";
    $form->add($field);

    // CAPTCHA - our form as argument, the function will add an InputfieldMarkup to our form
    $captcha->render($form);

    // add a submit button
    $submit = $this->modules->get("InputfieldSubmit");
    $submit->name = "submit";
    $submit->value = 'Submit';
    $form->add($submit);

    $out .= $form->render();

    // include javascript 
    $out .= $captcha->getScript();
}



echo $out;

 

  • Example using plain HTML Form :
$captcha = $modules->get("MarkupGoogleRecaptcha");
// if submitted check response
if ($captcha->verifyResponse() === true) {
	$out .= "Hi " . $input->post["name"] . ", thanks for submitting the form!";
} else {
	$out .= "<form method='post' action='{$page->url}'>\n"
         . "\t<input type='text' name='name'>\n"
         . $captcha->render() // render reCaptcha
         . "\t<input type='submit'>\n"
         . "</form>\n";

	$out .= $captcha->getScript();
}

echo $out;

 

 

captcha.thumb.png.68d131c777de7c108ef831fdb87c1c39.png

Edited by flydev
Update
  • Like 9
Link to comment
Share on other sites

  • 3 months later...

Hey,

I'm trying out this module on my site but for $captcha->verifyResponse() I am getting 1 not true?

I am workong on localhost which I have added to the domaine list with google so maybe thats the problem?

Thanks

Gar

Link to comment
Share on other sites

okay put it live and now getting

Parse error: syntax error, unexpected '[' in /var/www/vhosts/web1.plus-h.de/httpdocs/plus-h.de/site/assets/cache/FileCompiler/site/modules/MarkupGoogleRecaptcha/MarkupGoogleRecaptcha.module on line 6

Parse Error: syntax error, unexpected '[' (line 6 of /var/www/vhosts/web1.plus-h.de/httpdocs/plus-h.de/site/modules/MarkupGoogleRecaptcha/MarkupGoogleRecaptcha.module)

This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged.

Link to comment
Share on other sites

Looks like you're running PHP 5.3 on the live system. You could fix this error by replacing the short array notation [...] used throughout the module with its verbose notation as array(...), but you should really think of switching to a more current PHP release.

  • Like 5
Link to comment
Share on other sites

  • 4 months later...

I have a pull-request for this module, pull-request adding multi language support to reCAPTCHA and multiple usage on one page. You can see usage on screenshot.

For set reCAPTCHA language, (reCAPTCHA Language Codes) :

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->render($lang = "en"); }; ?>

For multiple reCAPTCHA usage, after all form render, call multiple usage script :

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->getScriptMulti(); }; ?>

 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Good day!

Thank you @flydev for the very useful module and @ukyo for making it possible to have multiple instances of reCAPCHA on the same page.

I am having just that: two forms on the same page both with reCAPCHA via the module. Both work using jQuery ajax. After an unsuccessful call I get response with error messages. Right here I need to reset reCAPCHA like described here in the JavaScript API section. But as I have two instances of reCAPCHA on the page I need to explicitly specify the opt_widget_id option. Is there a way to I get it with the module?

Link to comment
Share on other sites

You have an option, don't call getScriptMulti(); function.

<?php if(modules()->isInstalled('MarkupGoogleRecaptcha')) { echo modules()->MarkupGoogleRecaptcha->getScriptMulti(); }; ?>

Write your own javascript by referencing getScriptMulti(); function.

    public function getScriptMulti() {
        $return = "<script type=\"text/javascript\">
                    var onloadReCaptchaCallback = function(){
                        jQuery('.g-recaptcha').each(function() {
                            var _this = jQuery(this);
                            var recaptchaID = _this.data('id'),
                                hl = _this.data('hl'),
                                sitekey = _this.data('sitekey'),
                                theme = _this.data('theme'),
                                type = _this.data('type'),
                                size = _this.data('size'),
                                index = _this.data('index');
                            if(recaptchaID !== undefined) {
                                var recaptchaWidget = grecaptcha.render(recaptchaID, {
                                    'hl' : hl,
                                    'sitekey' : sitekey,
                                    'theme' : theme,
                                    'type' : type,
                                    'size' : size,
                                    'index' : index
                                });
                                grecaptcha.getResponse(recaptchaWidget);
                                // grecaptcha.reset(recaptchaWidget);
                            }
                        });
                    };
                </script>";
        $return .= "<script src='".self::SITE_RECAPTCHA_API_URL."?onload=onloadReCaptchaCallback&render=explicit' async defer></script>";

        return $return;
    }

 

  • Like 2
Link to comment
Share on other sites

Just bumped the module to version 2.

- Added  Google Invisible reCAPTCHA.

 

To use it, no change is necessary on the frontend, you just have to configure the module to use reCAPTCHA V2 or Invisible reCAPTCHA.

I will update the readme to reflect the change made by @ukyo and the new available options.

 

  • Like 3
Link to comment
Share on other sites

  • 3 weeks later...

@Lyndaa,

Moderator note

I have removed the link from your post. We do not allow links to third parties for first posters. This is to discourage spam (including subtle advertising). In fact, I don't see how your post is related to ProcessWire at all. I will revisit this and remove your post altogether unless you can otherwise show how it is related to ProcessWire.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I am wondering to add the reCAPTCHA to the login form of the Login-Register module.

I tried to make it by using the MarkupGoogleRecaptcha API in a hook, but without success:

// ready.php file
wire()->addHookAfter('LoginRegister::buildLoginForm', function($event) {

  $form = $event->return;

  // call the module and render reCAPTCHA
  $captcha = wire('modules')->get("MarkupGoogleRecaptcha");
  $captcha->render($form);

  // add JavaScript to the form
  $js = $captcha->getScript();
  // ... str_replace("</form>", "{$js}</form>", $form);

  // Call verifyResponse() to get the result.
  // ...

  $event->return = $form;

});

Furthermore, I do know if it is possible to add reCAPTCHA from within a hook.

Can someone help me in adding the reCAPTCHA to the login form of the Login-Register module?

Link to comment
Share on other sites

Hi @LAPS

try the following :  In ready.php put the following code :

$captchaobj = null;

    wire()->addHookProperty('LoginRegister::captcha', function ($event) {
        $event->return = wire('modules')->get("MarkupGoogleRecaptcha");
    });

    wire()->addHookAfter('LoginRegister::buildLoginForm', function ($event) {
        /* @var $form InputfieldForm */
        $obj = $event->object;
        $form = $event->return;
        $form->description = false; // remove the description
        $f = new InputfieldMarkup();
        $f->markupText = $obj->captcha->render();
        foreach ($form->children as $field) {
            if ($field instanceof InputfieldSubmit) { // find the submit button
                $form->insertBefore($f, $field);      // insert reCAPTCHA before the submit button
            }
        }

        $event->return = $form;
    });

    wire()->addHookBefore('LoginRegister::processLoginForm', function ($event) use (&$captchaobj) {
        $obj = $event->object;
        $captchaobj = $obj->captcha;
    });

    wire()->addHookBefore('Session::login', function ($event) use (&$captchaobj) {
        if (!is_null($captchaobj) && $captchaobj->verifyResponse() == false) {
            $event->arguments(0, '');
            $event->arguments(1, '');
            $event->arguments(2, false);
            $event->return = false;
        }
    });

 

Just one thing, I couldn't get the invisible mode to work with LoginRegister, only the reCAPTCHA v2.

edit:  a small hint about the getScript() :  you should call it from the main file, I mean, your "index.php" and only if the user isn't loggedin.

if(!$user->isLoggedin()) {
	echo $modules->get("MarkupGoogleRecaptcha")->getScript();
}

 

  • Like 1
  • Haha 1
Link to comment
Share on other sites

Hi @flydev,

thank you for you reply.

However, your code seems do not work. In particular, it seems do not output the JavaScript-related code ($captcha->getScript()). Can be this the problem?

Note: I am testing MarkupGoogleRecaptcha in localhost, so in Google Admin I added the domains 'localhost' and '127.0.0.1'.

LoginRegister form.png

Link to comment
Share on other sites

3 minutes ago, LAPS said:

In particular, it seems do not output the JavaScript-related code ($captcha->getScript()). Can be this the problem?

Yes it is. Read my last edit in my previous post.

 

Localhost for testing is fine, no problem with that.

Link to comment
Share on other sites

12 minutes ago, flydev said:

Good, now I think that your google challenge is not successful. Be sure that you saved the changes in your list of supported domains once you added the localhost.

In Google Admin I set 'localhost' and '127.0.0.1' (see attached image).

I tried to submit the login form from both 'http://localhost:8888/login' and 'http://127.0.0.1:8888/login' without success: I get the "Login failed" message.

 

Note: If it can help, in Google Admin I cannot set 'localhost:8888' nor '127.0.0.1:8888' since I get the error "URL starts with an invalid scheme".

Domains.png

Link to comment
Share on other sites

Did the challenge work ? I mean, the reCapatcha widget is "green-checked" ?

To be sure that the problem is not in the code I give you (I am currently using the same snippet on a website) ,  you should try to get the challenge working on a regular module call in a regular form. Can you test it  and report back ?

Link to comment
Share on other sites

14 minutes ago, flydev said:

Did the challenge work ? I mean, the reCapatcha widget is "green-checked" ?

Yes, the reCapatcha widget is "green-checked".

14 minutes ago, flydev said:

To be sure that the problem is not in the code I give you (I am currently using the same snippet on a website) ,  you should try to get the challenge working on a regular module call in a regular form. Can you test it  and report back ?

I tested it in a regular form, and it works as expected. It does not work just for the Login-Register login form.

Link to comment
Share on other sites

5 hours ago, flydev said:

Thank you - I will check, as now, I don't know why isn't working :lol:

OK, we are close to the solution.

If it can help, I am using:

  • ProcessWire v3.0.62
  • Login/Register v0.0.2 with option "Features to use" set to all (including "Use email address for login rather that user name") and option "Profile form fields" set to "E-mail Address (required)" and "Set Password (required)" more "First name" and "Last name".

Furthermore, after the login form submission (failed) in the Session logs I get these 2 rows:

2 seconds ago --- Error: Failed login for '' - Unknown user:
2 seconds ago --- Error: Failed login for 'my_email-email.com' - Unknown user: my_email-email.com

 

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

Hi @flydev. Great module. I'm using it on a custom form. I'd like to add it to an existing site where I've used Ryan's Form Template Processor module. (It's a long form; I'd rather not start from scratch). I've had a look at what you did with the log in form above; would I take a similar approach? Is there an easy way to slot the Captcha form in before the submit button (instead of outside the generated form)? I'm pretty new to hooks, if that's what's needed. Any suggestions appreciated.

  • Thanks 1
Link to comment
Share on other sites

hi @strandoo thanks you.

 

I never used the module you are talking about but I read the module code on Github and it look like you can insert the ReCaptcha before the submit button by hooking into the function ___buildForm().

Please try the following (not tested) and let me know. In ready.php put :

wire()->addHookAfter('FormTemplateProcessor::buildForm', function ($event) {
        /* @var $form InputfieldForm */
        $form = $event->return;
        $f = new InputfieldMarkup();
        $f->markupText = wire('modules')->get("MarkupGoogleRecaptcha")->render();
        foreach ($form->children as $field) {
            if ($field instanceof InputfieldSubmit) { // find the submit button
                $form->insertBefore($f, $field);      // insert reCAPTCHA before the submit button
            }
        }

        $event->return = $form;
    });

 

Link to comment
Share on other sites

Hi @flydev My, that was quick! I did put this in my ready.php file, but don't see a captcha. Do I need to include your '$captcha = $modules->get("MarkupGoogleRecaptcha");' on the page? (I've tried it both ways). I've also tried this $f->markupText = 'Foobar'; (instead of your module) and 'Foobar' appears above the submit button, as expected.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...