Yii, Ccaptcha. Captcha image doesnt show

Yii Ccaptcha

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

Version 1.1.x

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:
    public $verifyCode;
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')): ?>
<div class="row">
<?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()
return array(
// 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()
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
// need to allow captcha in order to view it.

That’s 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.


  1. If all that is written above is correct, but it still does not work, check your index file in the root directory. see if there extra spaces at the beginning of the file? they must be removed

  2. Thank you a million times Sahib Babiliyev. I had some meta tags in the first line of index.php. Could you explain why it breaks captcha but everything else works ?

  3. Sahib Babaliyev, many thanks for the removal of spaces to have sessions problems which caused the image to not appear. I have been debugging this for 10 days and the solutions was 1 seconds job !!

Leave a comment

Your email address will not be published. Required fields are marked *