简体   繁体   中英

How do I integrate hCaptcha with CakePHP for server-side verification and also make the captcha mandatory?

I am trying to follow the page below to set up hCaptcha on a series of sites running CakePHP.

https://polyte.de/en/php/2020/05/04/hcaptcha-cakephp.html

I am not much of a developer, so I am struggling with the server side verification part.

I have managed to get the hCaptcha element to display on the pages I want and you can click them and complete them.

eg https://www.yello.in/ - and then click Login or Sign-Up (this is controlled by header.ctp ).

Firstly, how can it be made mandatory? Currently you can skip it completely and still sign up or log in.

Secondly, and more importantly, based on the article above, how or where do I add the code for server-side verification? Looking at the code of my site I thought it would be in the UserController.php file.

This file contains functions for sign-up and login.

For example, the login function looks like this:

 PHP
 function login() {
        if (!empty($this->rememberUser)) {
            $this->redirect('/User/Profile');
        }
        if ($this->data) {
            $this->data = Sanitize::clean($this->data,  array('encode' => false, 'underscore' => false));
            $objUser = $this->User->find('first', array("conditions" => "(User.c_email='".$this->data['User']['c_email']."' or User.c_username='".$this->data['User']['c_email']."')
                        AND User.c_password='".$this->data['User']['c_password']."'"));
            if ($objUser) {
                if ($this->data['User']['c_remember'] != "0") {
                    $this->Cookie->write('Yello.User', $objUser, false, '1 year');
                } else {
                    $this->Session->write('Yello.User', $objUser);
                }
                $this->redirect('/');
            } else {
                $this->Session->setFlash(__d('user','incorrect-password',true),'default',array('class'=>'dvNotifyMsgFail'));
                $this->redirect('/User/login');
            }
        }

I want the hCaptcha verification to be verified on the login page, sign-up page and the page where you post an advert ( https://www.yello.in/post-ads ). I just don't know where to put the code. This is controlled by post.ctp .

The CakePHP version is 2.1.2.

Any help is much appreciated.

You can use this quick, drop-in cakephp2-hcaptcha plugin.

  1. Add echo $this->hCaptcha->challenge() to your $this->Form instance to display the challenge
  2. In your controller that receives the request, call $this->hCaptcha->verify($this->request->data) to verify the submission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM