Captcha 'get A New Code' Does Not Work

Hi all,

Instead of generating a fresh ‘captcha’ code when I click on ‘Get a new Code’ I get the below output:


{"hash1":744,"hash2":744,"url":"\/test\/site\/captcha?v=51491e4327c85"}

I am at a loss. I have gone through the forums but have drawn a blank. Has anyone else had this problem.

Output from Firebug:

3975

Screen shot 2013-03-20 at 1.40.gif

I am using Yii 1.1.13

My set up is below.

The instructions for setting up a Yii app seems pretty clear…

On the server the ‘framework’ files are in the the ‘private’ folder which is not accessible online.

This is the file structure:

httpdocs

.../test

… index.php

…/protected

httpsdocs

private

.../YiiRoot

…/framework

However when I set up the folders and files with the above instructions, the web application did not work.

Currently all folders and sub folders permissions are ‘rxwr-wr-w’.

Below are my code for for the website.

This is the code in my /test/protected/yiic.php file:


<?php


// change the following paths if necessary

$yiic=dirname(__FILE__).'/../../private/YiiRoot/framework/yiic.php';

$config=dirname(__FILE__).'/config/console.php';


require_once($yiic);



This is my ‘/test/index.php’ file contents.


<?php


// change the following paths if necessary

$yii=dirname(__FILE__).'/../../private/YiiRoot/framework/yii.php';

$config=dirname(__FILE__).'/protected/config/main.php';




require_once($yii);

Yii::createWebApplication($config)->run();

NB: All other Yii functionality works such as ‘crumbs’ and form error checking and sending emails.

The ‘.htaccess’ file in the same directory as the above ‘index.php’ file.


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -s [OR]

RewriteCond %{REQUEST_FILENAME} -l [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L]

RewriteRule ^.*$ index.php [NC,L]


SetEnv APPLICATION_ENV development

/config/main.php


		

		'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=>false,

			'rules'=>array(				

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',				

			),

		),



Model:


  public $name;

	public $email;

	public $phone;

	public $subject;

	 

	public $verifyCode;


	/**

	 * Declares the validation rules.

	 */

	public function rules()

	{

		return array(

			// name, email, subject and body are required

			array('name, email, phone', 'required'),

			// email has to be a valid email address

			array('email', 'email'),

			// verifyCode needs to be entered correctly

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

		);

	}



SiteController:


class SiteController extends Controller

{

	

	public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			// page action renders "static" pages stored under 'protected/views/site/pages'

			// They can be accessed via: index.php?r=site/page&view=FileName

			'page'=>array(

				'class'=>'CViewAction',

			),

		);

	}


	/**

	 * I have added a contact us form on the index page, which is a modified version of the standard ContactForm.

	 * 

	 */

	public function actionIndex()

	{

		$model=new ContactForm;

		if(isset($_POST['ContactForm']))

		{

			$model->attributes=$_POST['ContactForm'];

			if($model->validate())

			{

				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';

				$phone='=?UTF-8?B?'.base64_encode($model->phone).'?=';

				$headers="From: $name <{$model->email}>\r\n".

					"Reply-To: {$model->email}\r\n".

					"MIME-Version: 1.0\r\n".

					"Content-type: text/plain; charset=UTF-8";


				mail(Yii::app()->params['adminEmail'],$phone,$model->name,$headers);

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->refresh();

			}

		}

		$this->render('index',array('model'=>$model));		

	}

	

	/** 

	* @return array action filters 

	*/

	public function filters() 

	{ 

		return array( 'accessControl', // perform access control for CRUD operations 

		);

	}


/** * Specifies the access control rules. 

* This method is used by the 'accessControl' filter. 

* @return array access control rules 

*/

public function accessRules()

{


return array(

		array('allow', 

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

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

		), 

	);	

}



Finally: /views/site/index.php


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

	<div class="row">

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

		<div>

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

		<?php echo $form->textField($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>

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

	</div>

	<?php endif; ?>



I hope that this is comprehensive enough. Any help would be appreciated.

Thanks in advance.

Furthermore, what happens when I click on ‘Get a new code’ is that instead of just refreshing the captcha image it takes me to an entirely new page and just shows the output that the link creates.

When I hit the back button on the browser the captcha image has new characters.

The generated ‘url’ in the browser after clicking ‘Get a new Code’ is:

"www.somewebsite.com/test/site/captcha?refresh=1"

According to what I have read on the forums is that in the above ‘url’ “\/test” refers to the folder that the “index.php” file resides ie /test/ where the web app is launched.

And that "\/site" refers to the SiteController.

And “\/captcha” is an ‘action’ in the SiteController.

Why then is the whole page refreshed? Only the captcha image should be altered, leaving the rendered web page as is.

I have tried several things by commenting out portions of code in the CCaptcha.php file and also the CCaptchaAction.php files but to no avail.

What else can I do to rectify this problem?

Cheers

I have uploaded a sample Yii app on the host server and the issue does not occur. So I can rule out server side issues.

I’ve noticed that if I disable ‘//layouts/’ and just render the basic web page, the “Get a New Code” works.

I have a question regarding the way ‘layouts’ work in Yii. I have read the following information “Understanding the view rendering flow”, but in my quest to solve my problem I am more confused by the setup on Yii.

In ‘/protected/components/Controller.php’ I have commented out


public $layout='//layouts/column1';

and have removed the file ‘column1.php’ from the ‘/layout’ folder to ensure that it has no affect. But when I surf to the site, the page still renders as it should. Therefore, it seems that Yii is set up to look for a file called ‘main.php’ as a last resort?

If this is the case, why then all the fuss with the above setup?

I have used this extension , this is working fine for me, Try this one.

http://www.yiiframework.com/extension/captcha-extended/

Balu,

Thanks for your suggestion regarding using the captcha extension: it looks good, but I am still having the same problems with the link "Get a new code".

I finally found the problem:

This javascript file somehow affected the captcha code thus causing my problem.


<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery-1.2.6.min.js"></script>  

Even though I had commented out this code previously I had not noticed a change.

cheers

:D