Skip to content
[shareaholic app="share_buttons" id="14657658"]
Share this..

Logical Bot Check in CakePHP not CAPTCHA

2009 February 7
by Eddie

Spam is bad agreed? Of course. But making readers painfully decode swirled letters and numbers is nearly as bad. That is why many sites will use logic questions to prevent spam and bot activity.

Implementing logic questions on your mail forms and registration is easy for your readers and simple for you. This tutorial walks through the steps to create one using CakePHP.  The solution will pick  a random question and answer stored in the database and based on desired difficulty. Hooray!


There be spam bots trying to get your treasure!  Or at least submit annoying emails and comments.


Ask the kind of simple and random questions that a bot just wouldn’t understand.  Some of my favorites;

  • “Just one question – What is GOD spelled backwards?”
  • “Is this a question?”


So its all fine and dandy hard code a question into your forms, then hard code a check into your controllers, but it sure isn’t OO, and its definitely not CakePHP.

WHat if instead we threw a whole bunch of random questions into a DB, and pulled one each time someone went to a form?  Yes that would be nice indeed.  We will need to hold  a corresponding answer as well.

THere are a few parts to this solution, so pay attention.

  • Managing site Settings
    • Model
    • Controller
    • Views
  • Managing the Questions and Answers
    • Model
    • Controller
    • Views (add/edit etc.)
  • Supplying the Q&A to forms
    • BeforeFilter method

Managing Site Settings – Settings

Now this MVC group is optional.  I deliver a lot of prototypes to clients and found that asking them to edit any source file just introduces more problems.  For example if they want to change the site’s title, or tagline, or other metadata. TO solve this I provide a simple Settings model that stores these values in a user friendly way.

Read how to use a Global Settings Component for your CakePHP Site

If it is a personal site or you just don’t care for this you’ll need to adjust  how we obtain in the difficulty levels in the next model.

Managing Questions and Answers – BotCheck

At the heart of my solution is a Model. I named it BotCheck, but you could call it PussNBoots for all I care.


class Botcheck extends AppModel {
	var $name = 'Botcheck';
	var $validate = array(
		'question' => VALID_NOT_EMPTY,
		'answer' => VALID_NOT_EMPTY,
		'difficulty' => VALID_NOT_EMPTY,
	var $recursive = -1;	
	var $minDiff=1;
	var $maxDiff=10;
 * set level to use
	function setMinMax($minMax=array()){	
		//import settings model to pull from method getBotcheckMinMax
		$ms=new Setting();
		//now set the locAL varable 
	 * Grabs a random question and answer from the database. 
	 * Only use minmaxoverride to circumvent table settings.
	 * @param array minMaxOverride
	function getFreshBotcheck($minMax=null){			
		//we need to load the settings form the settings table/model
		// set conditions for highest and lowest difficulty to pull
		$conditions=array('difficulty >='=>$this->minDiff,'difficulty <='=>$this->maxDiff);		
		//get nuimber of rows that match
		//use random number as page (doesnt depend on record ids)
		//we let answers be sets seperated by commas, make it an array after making it all lowercase
		//simplify results into a ['question'] and ['answer'] keyed variable for direct pull
		return $fbc;


class BotchecksController extends AppController {
	var $name = 'Botchecks';
	var $helpers = array('Html', 'Form');
	var $uses = array('Botcheck','Setting');
	function index(){
	function admin_index() {
		$this->Botcheck->recursive = 0;
		$this->set('botchecks', $this->paginate());
		$this->set('bcMin',Configure::read('App.botcheck_min_difficulty') );
		$this->set('bcMax',Configure::read('App.botcheck_max_difficulty') );
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Botcheck.', true));
		$this->set('botcheck', $this->Botcheck->read(null, $id));
	function admin_add() {
		if (!empty($this->data)) {
			if ($this->Botcheck->save($this->data)) {
				$this->Session->setFlash(__('The Botcheck has been saved', true));
			} else {
				$this->Session->setFlash(__('The Botcheck could not be saved. Please, try again.', true));
	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Botcheck', true));
		if (!empty($this->data)) {
			if ($this->Botcheck->save($this->data)) {
				$this->Session->setFlash(__('The Botcheck has been saved', true));
			} else {
				$this->Session->setFlash(__('The Botcheck could not be saved. Please, try again.', true));
		if (empty($this->data)) {
			$this->data = $this->Botcheck->read(null, $id);
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Botcheck', true));
		if ($this->Botcheck->del($id)) {
			$this->Session->setFlash(__('Botcheck deleted', true));


These are standard CakePHP views (view/add/edit/delete) that can be baked via the console based on the above controller.
The views will allow you, or the application admin to add lots random questions and answers. THe nice thing is that answers are an array. So you can ask “what do plants need to survive?” and accept “air” or “oxygen” or “water”..etc. etc.

Supplying Random Logic Questions to User Forms

Now we have a bunch of questions that are ready to be loaded at will. We will take a look at my User’s controller to see how we pull a random question and answer pair and store them to session. This way if the user forgets there email, or another form field we can hang on to the same question and answer. No reason to make them feel like they are on quiz show.

app/controllers/users_controller.php – snippet

pageTitle= ' // '.Inflector::humanize(Inflector::underscore($this->name)).' '.$this->action;
	function beforeFilter(){
		//die('Not empty?: '.!empty($this->data));
		//grab a fresh botcheck question from the db
		//UNLESS there is already one in session AND there is data being passed from a form
		if($this->Session->check('botcheck') && !empty($this->data)){
			//die('we got your stickin session');
			//This indicates we are likely just checking info entered, 
			//be sure to use the same botcheck (hard for users to psychic.)
			//we must get a unique botcheck, since one does not exist, or it is likley from a past event
			//save it for validation once they attempt to answer
		//set it to a variable used by actions
	function notify_friends_and_family($wishListId){
		$wishListArr=$this->User->WishList->find(' = '.$wishListId);
		if (!$wishListId && empty($this->data)) {
			$this->Session->setFlash('Um... Like, OK.  I dont\'t wanna be  buzzkill, but like, you totally need to need to visit that page from a wish list. Otherwise I am like totally unable to help you, K?', true);
			$this->Session->setFlash('Wow! Your totally trying to notify people of a list you um, like don\'t own.  Maybe you should like come here from one of your list. Then you like, don\'t even need to worry and stuff, K?', true);
		//is submitted?
		if (!empty($this->data)) {
			//set modified dates to now
			if ($merror==0) {
				$wishListArr=$this->User->WishList->find(' = '.$this->data['WishListId']);
				$this->Session->setFlash(__('Your messages were sent.', true));
			} else {
				$this->Session->setFlash(__('Please Correct These Errors', true));
		// grab botcheck question

Notice 3 things;

  1. We declare our BotCheck model in the ‘Uses’ line
  2. We set default values for min and max
  3. We add some logic in Before Filter to retrieve a fresh pair if it is a freshly visited form

The method included (notify friends and family ) allows users to send emails to up to 6 addresses at once. Although they can’t adjust the message, its still worth keeping spam bots out.


Ok, lets let everyone know about your

OR, link('Go right to your list','/wish_lists/view/'.$wishlist['WishList']['id']);?>, or link('add your first item','/items/add/'.$wishlist['WishList']['id']); ?>

input('email.First Recipient'); echo $form->input('email.2'); echo $form->input('email.3'); echo $form->input('email.4'); echo $form->input('email.5'); echo $form->input('email.6'); echo $form->input('WishListId',array('type'=>'hidden','value'=>$wishlist['WishList']['id'])); ?>

input('User.check', array('size'=>'10','label'=>$botQuestion,'error'=>'Are you human?'));?>

  • link(__('List WishLists', true), array('action'=>'index'));?>
  • link(__('List Users', true), array('controller'=> 'users', 'action'=>'index')); ?>
  • link(__('New Owner', true), array('controller'=> 'users', 'action'=>'add')); ?>
  • link(__('List Items', true), array('controller'=> 'items', 'action'=>'index')); ?>
  • link(__('New Item', true), array('controller'=> 'items', 'action'=>'add')); ?>


SO now when someone lands on a page that we are checking they see this;
Bot Check - Logic Question

One Response leave one →
  1. March 14, 2009

    Nice done!

    Thanks for sharing this code, it’s a great and reusable solution..



Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS