编程语言
首页 > 编程语言> > PHP-如何在Phalcon框架中使用Recaptcha Google

PHP-如何在Phalcon框架中使用Recaptcha Google

作者:互联网

我仍在尝试向我的网站添加一个Recaptcha,我想尝试Google的Recaptcha,但是我不能正确使用它.是否检查,我的电子邮件仍然发送.

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

但是我不明白我的问题在哪里,更不知道如何创建像

 $recaptcha = new Check('recaptcha');

我的控制器实现:

    <?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论坛上很少有人推荐它.

标签:phalcon,recaptcha,php
来源: https://codeday.me/bug/20191118/2029049.html