CakePHP Captcha Plugin

Demo
Installation

CakePHP 1&2
CakePHP 3

Downloads

CakePHP 1&2
CakePHP 3

Developers

Arvind Kumar
@Inimist Technologies

Need Help?

Get in touch
Call +919876831310

CakePHP Captcha Plugin by Inimist Technologies is one of the most used, stable and trusted CakePHP captcha plugin support since 2009. We have maintained CakePHP Captcha support throughout these long years and we are still committed to maintain it as long we found it useful for the large community of CakePHP framework.

CakePHP Image Captcha Demo

Can't read? Reload
[Using default theme. Use random theme for variations]

{Source Code: CakePHP Captcha, Image Challenge}

(Code with yellow background in the source code below indicates the only changes to your application)

Call Captcha behavior in the model ie Table class UsersTable.php in this example


public function initialize(array $config)
{
	parent::initialize($config);
	$this->addBehavior('Captcha.Captcha', ['field'=>'captcha_input_field_name']);
}

Place in form, in view file register.ctp in this example

<?= $this->Form->create($user, ['novalidate']) ?>
	<fieldset>
		<legend><?= __('CakePHP Image Captcha Demo') ?></legend>
		<?php
		echo $this->Form->input('name');
		echo $this->Captcha->create('captcha_input_field_name', [
		'type'=>'image',
		'theme'=>'random' //two themes, "default" and "random",
		'width'=>220,
		'height'=>90,
		]);
		?>
	</fieldset>
	<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
	jQuery('.creload').on('click', function() {
		var mySrc = $(this).prev().attr('src');
		var glue = '?';
		if(mySrc.indexOf('?')!=-1) {
			glue = '&';
		}
		$(this).prev().attr('src', mySrc + glue + new Date().getTime());
		return false;
	});
</script>

Call Captcha component in the initialize function of the conroller, UsersControllerr.php in this example


public function initialize()
{
	parent::initialize();
	$this->loadComponent('Captcha.Captcha');
}

Place in UsersControllerr.php action, "register" here

public function register()
{
	$user = $this->Users->newEntity();
	if ($this->request->is('post')) {
		$this->Users->setCaptcha('captcha_input_field_name', $this->Captcha->getCode('captcha_input_field_name'));
		$user = $this->Users->newEntity($this->request->getData());
		if ($user->errors()) {
			$this->Flash->error(__('Validation failed.'));
		}	else	{
			$this->Flash->success(__('Validation success.'));
		}
	}
	$this->set(compact('user'));
}

That should be it!

If you should have any proble, question, comment, feedback or any suggestion for CakePHP Captcha Plugin please feel free to drop us a message at one of our contact us page or at Github