在SilverStripe表单上添加Google Recaptcha

时间:2017-01-08 18:36:22

标签: captcha recaptcha silverstripe

我正在尝试将Google Recaptcha添加到我的自定义SilverStripe表单中。

我已经生成了Google公钥和私钥,但我不知道在哪里将它们放在我的网站上显示验证码。

这是我目前的代码:

ContactPage

class ContactFormSubmission extends DataObject {
    private static $db = array(
        'FirstName' => 'Text',
        'LastName' => 'Text',
        'Email' => 'Text',
        'Phone' => 'Text',
        'Iam' => 'Text',
        'Subject' => 'Text',
        'Message' => 'Text'
    );
}

ContactFormSubmission

lowercase = ('a'..'z')
uppercase = ('A'..'Z')
input.each_char.any?{ |char| lowercase.cover?(char) || uppercase.cover?(char) }

如何正确地将Google Recaptcha添加到我的表单中?

2 个答案:

答案 0 :(得分:2)

我们可以使用SilverStripe Nocaptcha module将Google Nocaptcha添加到我们的表单中。

安装此模块的最简单方法是通过composer使用以下命令:

composer require undefinedoffset/silverstripe-nocaptcha

安装模块后,请务必致电dev/build?flush=all

接下来,我们必须通过我们的网站config.yml文件将垃圾邮件保护程序设置为NocaptchaProtector,并设置Nocaptcha密钥。

<强> mysite的/ _config / config.yml

# ...

FormSpamProtectionExtension:
  default_spam_protector: NocaptchaProtector

NocaptchaField:
  site_key: "YOUR_SITE_KEY"
  secret_key: "YOUR_SECRET_KEY"

设置new Nocaptcha account时,会通过Google检索密钥。

确保在添加这些设置后致电?flush=all

我们现在准备在我们的表单上启用垃圾邮件防护。为此,我们只需在表单函数中调用$form->enableSpamProtection()

public function ContactForm()
{
    // Create fields
    $fields = FieldList::create(
        // ...
    );

    // Create actions
    $actions = FieldList::create(
        // ...
    );

    $validator = ZenValidator::create();

    $form = Form::create($this, 'ContactForm', $fields, $actions, $validator);

    $form->enableSpamProtection();

    return $form;
}

现在应该在我们的表单上启用Google Nocaptcha。

答案 1 :(得分:0)

添加

new LiteralField('recaptcha_bubble', '<div id="recaptcha_bubble" class="field"></div>')

在您的表单字段列表中 然后使用

添加google javascript
Requirements::javascript("https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&hl=en-GB");

之后将功能添加到您的javascript

var recaptchaCallback = function () {
var elementExists = document.getElementById('recaptcha_bubble');
if (null != elementExists) {
    grecaptcha.render('recaptcha_bubble', {
        'sitekey' : 'the site key here'
    });
}};

当表单将与变量'g-recaptcha-response'

一起提交时
function getPostUrlContents($url, $fields){
    $result = null;

    $ch = curl_init();
    $timeout = 30;

    $fields_string = '';
    foreach($fields as $key=>$value) {
        $fields_string .= $key.'='.$value.'&';
    }

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($fields_string, '&'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $result = curl_exec($ch);

    curl_close($ch);
    return $result;
}

public function checkGoogle_recaptcha_Response($code) {
    $result = $this->getPostUrlContents(
        'https://www.google.com/recaptcha/api/siteverify',
        array(
            'secret' => urlencode('secret key here'),
            'response' => urlencode($code)
        )
    );
    if (!$result) return false;
    $gResult = json_decode($result, true);
    if ($gResult['success']) {
        return true;
    }

    return false;
}

public function yourForm(SS_HTTPRequest $request) {

    $vars = $request->postVars();
    if (!$this->checkGoogle_recaptcha_Response($vars['g-recaptcha-response'])) {
        // throw error
    } else {
        //not a robot, do something with request
    }
}