Category Archives: CakePHP

Simple captcha component for CakePHP 1.x

Updated on – April 11, 2013

Download Captcha Component for CakePHP 1.x

Download Captcha Component for Cakephp 2.x

Model validation to validate captcha field value included with examples. Functions(rules) required for model validation are also written in the model file.
———————————————————-

Follow these simple steps to make it working.

Copy attached font file (monofont) to ‘webroot’ folder.

Copy component file (captcha.php) to app/controllers/components

Create a function similar to the following in your controller. I have used “signups” controller for demonstration purpose. You should change controller name accordingly.

function captcha()	{
		$this->autoRender = false;
		$this->layout='ajax';
		if(!isset($this->Captcha))	{ //if Component was not loaded throug $components array()
			App::import('Component','Captcha'); //load it
			$this->Captcha = new CaptchaComponent(); //make instance
			$this->Captcha->startup($this); //and do some manual calling
		}
		//$width = isset($_GET['width']) ? $_GET['width'] : '120';
		//$height = isset($_GET['height']) ? $_GET['height'] : '40';
		//$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
		//$this->Captcha->create($width, $height, $characters); //options, default are 120, 40, 6.
		$this->Captcha->create();
	}

Call the captcha action from within your img tag in the view file as below:

echo $form->create("Signups");
echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?>
echo 'Enter the given security code:';
echo $form->input('Signup.captcha',array('autocomplete'=>'off','label'=>false,'class'=>'','error'=>__('Failed validating code',true)));
echo $form->submit(__(' Submit ',true));
echo $form->end();

And you are done. The captcha image should look like Cakephp Captcha COmponent

Also, you can re-generate the captcha by adding (replacing code in line 2 of the above) ajax code something similar to the following:

<div id="captchaID" ><?php
echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?></div>
<?php echo $ajax->link('Can not read this code? Reload.','regenerate code',array('url'=>'reload_captcha','update'=>'captchaID')); ?>

In the code above it calls a reload_captcha function of the current controller. You can place reload_captcha in current controller or preferrably in the app_controller to make it available universally. Here’s the code:

function reload_captcha()	{
		App::import('Component','Captcha'); //load it
		$this->Captcha = new CaptchaComponent(); //make instance
		$this->Captcha->startup($this); //and do some manually calling
		$this->layout='ajax';
		Configure::write('debug',0);
		$this->viewPath = 'elements'.DS;
		$this->render('reload_captcha');
	}

It renders a reload_captcha element. Create a reload_captcha.ctp file and place it in views/elements with the following code:

<?php echo $html->image($html->url(array('controller'=>'signups', 'action'=>'captcha'), true),array('style'=>'','vspace'=>2)); ?>

There is model validation code included in the files with example of how to validate it. Files include a model with new validation entry along with two rules (functions).

Download Captcha Component for Cakephp 1.x
Download Captcha Component for Cakephp 2.x

Customizing mysql query of a bound model

I had a hasAndBelongsToMany binding in my Charity Model as i normally would need all the “Causes” available along with Charity information and vice-versa. At one place i needed a specific number of “Causes” picked randomly from the database table. To make this work i had to add following two lines just before the $this->Charity->find call in my controller action:

$this->Charity->hasAndBelongsToMany['Cause']['limit']=5;
$this->Charity->hasAndBelongsToMany['Cause']['order']='rand()';

and i was done.

Does someone has a better solution? Please suggest.

Creating Ajax form in Cakephp (with Prototype.js)

(If you are using jQuery with cakephp you may want to view this article, Creating Ajax form in Cakephp using jQuery )

With the help of prototype/JavaScript we can build really cool and fully functional ajax form in CakePHP really fast.

I have been using the ajax forms in CakePHP for a long time now but in the view of continuous development work always on at CakePHP framework development community i have had to alter ways to write the same code. So here is the latest one. Continue reading

What do you hate about CakePHP?

After looking at an interesting Google group topic
Poll: what do you hate about CakePHP? i have tried to sum up the facts which people reasoned for hating/disliking CakePHP. Please be noted that all the facts listed here are based on users postings in the very topic and I have not tried to reproduce/test them all in a working environment. So, please Continue reading

getid3 component for cakephp

What is getID3?

getID3() is an open-source, cross-platform software library for the PHP language written by James Heinrich and Allan Hansen. getID3() can extract information from multimedia file formats: audio, video and images in various formats. Information extracted includes playtime, metadata, bitrate, sample rate, number of channels, and much more. getID3() can also write metadata (tags) to several formats. Continue reading

An alternate to paginator counter showing page in cakephp

An ordinary yet useful piece of hack to the way the cakephp pagination’s pages “showing page” parameter is displayed. As default, we are used to use $paginator->counter() method which gives us a counter string for the paged result set like “Showing page 1 of 3”. But in my latest project task i needed to show results set something like “Showing 1 to 20 of total 46”. Continue reading