tu如何使用phalcon框架重新学习google

时间:2016-06-08 14:35:44

标签: php recaptcha phalcon

我仍在尝试将recaptcha添加到我的网站,我想尝试从Google重新收回,但我无法正确使用它。检查与否,我的电子邮件仍然发送。

我试图理解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:我道歉,因为我在这里是一个菜鸟,我的母语不是英语,所以如果你不理解我或想给我一些建议来提出正确的问题,请不要犹豫^^

1 个答案:

答案 0 :(得分:3)

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

表单元素类:

|decisionId| voterId  |               |voterId  | userId  |  weight
|----------|--------- |               |---------|---------|----------
|1         | 1        |               |1        | 2       | 10.2
|1         | 2        |               |2        | 3       | 2.7
|1         | 3        |               |3        | 5       | 8.1
|1         | 4        |               |4        | 6       | 20.1
|1         | 5        |               |5        | 8       | 15.6
|1         | 6        |               |6        | 9       | 11.2
|2         | 7        |       and     |7        | 1       | 52.6
|2         | 8        |               |8        | 3       | 84.2
|2         | 9        |               |9        | 7       | 84.3
|3         | 10       |               |10       | 5       | 12.2
|3         | 11       |               |11       | 7       | 54.6
|3         | 12       |               |12       | 8       | 74.6
|3         | 13       |               |13       | 9       | 96.3

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

在表单类中使用:

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

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

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