Captcha In Yii Framework Don't Appear (Show)

The Complete Solution.

Add the following in the controller




	public function actions()

	{

		return array(	// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

        	);

	}


	public function accessRules()

	{

		return array(

			array('allow', 'actions'=>array('captcha'),

                		'users'=>array('*'),

            		),

		);

	}




In model

a. Define the variable




public $verifyCode;



b. Add variable in access rules




        public function rules()

	{

        	return array(

                        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),

		);

	}



In the view file




<?php if(CCaptcha::checkRequirements()): ?>

	<div class="row">

		<div>

			<?php echo $form->labelEx($model,'verifyCode'); ?>

			<?php $this->widget('CCaptcha'); ?>

			<?php echo $form->textField($model,'verifyCode'); ?>

			<?php echo $form->error($model,'verifyCode'); ?>

		</div>

		<div class="hint">Please enter the letters as they are shown in the image above.

		<br/>Letters are not case-sensitive.</div>

	</div>

<?php endif; ?>



Sometimes (actually many times) this bug is because of a unicode BOM at the top of a file within our runtime.

If the bug is about BOM :

The most important note we must be aware about it is the headers_sent() which will return false even if you have a BOM has been sent in text/html content type (which false means no header has been sent yet which is not the real answer). This happened for me via PHP 5.5.6 on windows 7 (xampp)

Thus how you can find whether the BOM has been sent or not… I used exit(); within my code either in my module class (extending CWebModule) especially in my beforeControllerAction and I used my firebug to find is there any byte in the server response or not.

The result must be 0B but if there is a BOM you will have 3B and the code in your response.

My problem was about my language files have been edited one time in a notepad window (netbeans will not add BOM but notepad will add).

EDITED :

You may have no bytes (no BOM) in the first of actions method of your controller (because it is only the declaration). I had tracked the bug even to the CCaptchaAction Class to the header(‘Content-Type: image/png’). And there I realized headers_sent() says there is not header has been sent.