Captcha refers to the imagery device ment to capture bots attempting to fill out your forms for automatic spam submission. It auto generates images with codes that users have to input to submit a form to avoid such spam bot issues. The only real forms that would require this device would be forms accessible to none users of your site, all other forms should be filtered by needing access to your site from a membership.
Common Captcha Problems
Captcha Image doesn’t load on page?
The first step is to see if the gd extension library is installed and functional for your php engine to read and display the image file. To do this, make a php file with the following code:
This will tell you all the specs of your php install.
gd extension is there and working, now what?
If the gd extension is operational then the error lies within your code. Let’s go through this step by step:
Did you define a public variable & rule set for the verification code?
Regardless if you’re using a case of CForm or CActiveRecord or any other class you have extended (or made yourself), you need to define a variable to hold the captcha code. You will also have to define the model’s rules regarding the captcha validation.
In your model define the public variable:
Then define the rule set:
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
The variable is set, the rules are set, hello still not working!?
Ok so you’ve made the needed changes in the model the next step is to check out the view and the controller files.
Is your view file setup properly?
Your view file should replicate what is in the default web app contact form view file. Here it is below:
<?php if(extension_loaded('gd')): ?>
<?php echo $form->labelEx($model,'verifyCode'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php endif; ?>
I already had that dammit! Why isn’t it working!?
We’ll be honest, we knew you did, you probably had most of this up untill this point. But we like to annoy you and go through every step. Stupid, we know, but fun on our part if you didn’t noticed ;). The last stage is where most people forget the code to place as it’s within the controller. The code here is not so obvious. You need to override the actions function to include a captcha reference. You also need to permit in the access control filter (if you’re using one) guests to access the captcha action. So let’s do this:
Override the actions function
* Declares class-based actions.
public function actions()
// captcha action renders the CAPTCHA image displayed on the register page
Next we need to permit access to the captcha action for guests, that is if you have defined an access control filter and have the access control rules set. This part would go in the rules:
public function accessRules()
array('allow', // allow all users to perform 'index' and 'view' actions
// need to allow captcha in order to view it.
That’s it. Often it’s the easier things that give you the most trouble. Feel better? We hope so and if this wasn’t the solution to your problem, as always try the Yii Forums for more specific solutions.