我仍在尝试向我的网站添加验证码,我想尝试使用 Google 的验证码,但我无法正确使用它。检查与否,我的电子邮件仍然发送。

我试图理解 How to validate Google reCaptcha v2 using phalcon/volt forms? 的代码。

但我不明白我的问题在哪里,更多的是你如何创建一个像

 $recaptcha = new Check('recaptcha');

我的 Controller 实现:
    <?php

/**
 * ContactController
 *
 * Allows to contact the staff using a contact form
 */
class ContactController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('Contact');
        parent::initialize();
    }

public function indexAction()
{
    $this->view->form = new ContactForm;
}

/**
 * Saves the contact information in the database
 */
public function sendAction()
{
    if ($this->request->isPost() != true) {
        return $this->forward('contact/index');
    }

    $form = new ContactForm;
    $contact = new Contact();

    // Validate the form
    $data = $this->request->getPost();
    if (!$form->isValid($data, $contact)) {
        foreach ($form->getMessages() as $message) {
            $this->flash->error($message);
        }
        return $this->forward('contact/index');
    }

    if ($contact->save() == false) {
        foreach ($contact->getMessages() as $message) {
            $this->flash->error($message);
        }
        return $this->forward('contact/index');
    }

    $this->flash->success('Merci, nous vous contacterons très rapidement');
    return $this->forward('index/index');
}

}

在我看来,我补充说:
<div class="g-recaptcha" data-sitekey="mypublickey0123456789"></div>
{{ form.messages('recaptcha') }}

But my problem is after : i create a new validator for the recaptcha like in How to validate Google reCaptcha v2 using phalcon/volt forms? :

use \Phalcon\Validation\Validator;
use \Phalcon\Validation\ValidatorInterface;
use \Phalcon\Validation\Message;

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute)
    {
        if (!$this->isValid($validation)) {
            $message = $this->getOption('message');
            if ($message) {
                $validation->appendMessage(new Message($message, $attribute, 'Recaptcha'));
            }
            return false;
        }
        return true;
    }


    public function isValid($validation)
    {
        try {

            $value =  $validation->getValue('g-recaptcha-response');
            $ip    =  $validation->request->getClientAddress();

            $url = $config->'https://www.google.com/recaptcha/api/siteverify'
            $data = ['secret'   => $config->mysecretkey123456789
                     'response' => $value,
                     'remoteip' => $ip,
                    ];

            // Prepare POST request
            $options = [
                'http' => [
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'  => 'POST',
                    'content' => http_build_query($data),
                ],
            ];

            // Make POST request and evaluate the response
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            return json_decode($result)->success;
        }
        catch (Exception $e) {
            return null;
        }
    }
}

So i don't know if tjis code is correct anyway, i have a problem too after that : how to create an object "recaptcha" in my form add

$recaptcha = new ?????('recaptcha');
        $recaptcha->addValidator(new RecaptchaValidator([
            'message' => 'Please confirm that you are human'
        ]));
        $this->add($recaptcha);

PS:我很抱歉,因为我在这里是个菜鸟,而且我的母语不是英语,所以如果你不明白我的意思或者想给我一些建议来创建一个合适的问题,不要犹豫^^

最佳答案

我为 recaptcha 制作了一个自定义表单元素。到目前为止,它已用于许多项目。

表单元素类:

class Recaptcha extends \Phalcon\Forms\Element
{
    public function render($attributes = null)
    {
        $html = '<script src="https://www.google.com/recaptcha/api.js?hl=en"></script>';
        $html.= '<div class="g-recaptcha" data-sitekey="YOUR_PUBLIC_KEY"></div>';
        return $html;
    }
}

recaptcha 验证器类:
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class RecaptchaValidator extends Validator implements ValidatorInterface
{
    public function validate(\Phalcon\Validation $validation, $attribute)
    {
        $value = $validation->getValue('g-recaptcha-response');
        $ip = $validation->request->getClientAddress();

        if (!$this->verify($value, $ip)) {
            $validation->appendMessage(new Message($this->getOption('message'), $attribute, 'Recaptcha'));
            return false;
        }
        return true;
    }

    protected function verify($value, $ip)
    {
        $params = [
            'secret' => 'YOUR_PRIVATE_KEY',
            'response' => $value,
            'remoteip' => $ip
        ];
        $response = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?' . http_build_query($params)));

        return (bool)$response->success;
    }
}

在你的表单类中使用:
$recaptcha = new Recaptcha($name);
$recaptcha->addValidator(new RecaptchaValidator([
    'message' => 'YOUR_RECAPTCHA_ERROR_MESSAGE'
]));

注 1: 你快到了,你只是错过了创建自定义表单元素(我的例子中的第一个和最后一个代码段);

注 2: 在 Github 上也有一个库:https://github.com/fizzka/phalcon-recaptcha 我没有用过,但是在 phalcon 论坛上很少有人推荐它。

关于php - 如何在 phalcon 框架中使用 recaptcha google,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37705568/

10-15 01:06