Captcha validation doesn't work with regular forms.

Hello, everybody.

I’d like to sorry for my English at first.

I have a problem which makes me crazy for the last days.

I can’t make captcha validation to work. The problem is that on these sites they use not a CActiveForm. I’ve made a duct tape which just checks validation code in action and redirects back if code is wrong (It’s awful).

I’ve searched the all web and tried a lot of ways, but nothing work ;( I don’t have a lot of experience in PHP, so that’s a problem for me.

I’ll be very thankful for any help.

So here’s code.

One example of form (there are a lot of them, but they are equal):


<?php $model = new Page();?>

<form id="stop-subscription" action="/supportcontact" method="post">

 

<?php if(CCaptcha::checkRequirements() && Yii::app()->user->isGuest):?>

<input type="hidden" name="subject" value="<?=$this->t('stop-subsc',false);?>"/>

<h2><?=$this->t('stop-subsc');?></h2>

<label for="name"><?=$this->t('username');?></label>

<input id="name" name="name" type="text">

<label for="email"><?=$this->t('email');?></label>

<input id="email" name="email" type="text" required>

<p><?=$this->t('email-usage');?></p>

<label class="msg" for="message"><?=$this->t('message');?></label>

<textarea id="message" name="message" type="text" required></textarea>

 

<?php echo CHtml::activeLabelEx($model, 'verifyCode');?>

<?php $this->widget('CCaptcha',array('captchaAction' => '/page/page/captcha',))?>

<?php // echo CHtml::activeTextField($model, 'verifyCode')?>

<?php //CHtml::TextField('captcha'); ?>

<input type="text" name="captcha" required/>

 

<?php endif?>

<a class="button blue" href="#send"><?=$this->t('send');?></a>

</form>

PageController:


 

...

public function actionSupportcontact()	

{

                                            $model = new Page();

                                           $captcha=$this->createAction("captcha");

                                           $code = $captcha->verifyCode;

                                          

		$loc = '';

                                            

		$ref = yii::app()->getRequest()->getUrlReferrer();


                                            if($code === $_REQUEST['captcha']){ 

		$zone = substr(parse_url($ref, PHP_URL_HOST), -3);

		if ('com' == $zone)

		{

			$path = parse_url($ref, PHP_URL_PATH);

			if ('/' == $path{3})

			{

				$target = substr($path, 0, 3) . $target;

				$loc = $path{1} . $path{2};

			}

		}

		// @todo check hostname

		else

		{

			$loc = $zone{1} . $zone{2};

		}

		$email_to = 'support' . $loc . '@movavi.com';


		$email_from = $_POST['email'];

		$email_subject = isset($_POST['subject']) ? $_POST['subject'] : "Feedback Form From Movavi";

		$email_message = "";


		$email_message .= isset($_POST['name']) ? 'Name: ' . $_POST['name'] . "\n" : "";

		$email_message .= isset($_POST['email']) ? 'Email: ' . $_POST['email'] . "\n" : "";

		$email_message .= isset($_POST['software']) ? 'Movavi software: ' . $_POST['software'] . "\n" : "";

		$email_message .= isset($_POST['version']) ? 'Version: ' . $_POST['version'] . "\n" : "";

		$email_message .= isset($_POST['settings']) ? 'Output settings: ' . $_POST['settings'] . "\n" : "";

		$email_message .= isset($_POST['opsystem']) ? 'Operation system: ' . $_POST['opsystem'] . "\n" : "";

		$email_message .= isset($_POST['source']) ? 'Original video source: ' . $_POST['source'] . "\n" : "";

		$email_message .= isset($_POST['device']) ? 'Device: ' . $_POST['device'] . "\n" : "";

		$email_message .= isset($_POST['message']) ? 'Message: ' . $_POST['message'] . "\n" : "";


		// if files uploaded

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

		{

			$email_message .= "\nHere are uploaded files: \n";

			$cnt = 1;

			foreach ($_POST['uploaded_files'] as $id => $uploaded_file)

			{

				$filename = $uploaded_file["name"];


				$email_message .= "Uploaded file #$cnt: " . rawurldecode($uploaded_file["url"]) . "\n";

				$cnt++;

			}

		}

		$this->sendMail($email_to, $email_subject, $email_message, $email_from);

//                                          

		$this->redirect($target);

                                            } else {

                                                

                                                $this->redirect($ref);

                                                

                                            }

	

}

    public function filters() {

        return array(

            'accessControl',

        );

    }

    public function accessRules() {

        return array(

            // если используется проверка прав, не забывайте разрешить доступ к

            // действию, отвечающему за генерацию изображения

            array('allow',

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

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

            ),

          

        );

    }


    public function actions(){

        return array(

            'captcha'=>array(

               'class'=>'CaptchaExtendedAction',

                'testLimit' => 0,

           ),

        );

    }

Model Page:




...

public $verifyCode;

....

public function rules()

{

   return array(

      array('url', 'required'),

      array('url', 'length', 'min' => 2),

      array('parent,product, r_product', 'numerical'),

      array('r_category', 'length', 'max' => 50),

      array('localizations', 'safe'),

      array('isDir', 'in', 'range' => array(0, 1)),

      array(

         'verifyCode',

         'captcha',

         'allowEmpty' => !CCaptcha::checkRequirements(),

         'captchaAction' => 'page/page/captcha',

      ),

 

);

}



Hi lexzotov

I didn’t read all of your code but the below line


<?php $this->widget('CCaptcha',array('captchaAction' => '/page/page/captcha',))?>

seems to use an action that not exist

ControllerPage -> (actionPage ?)

Please try


<?php $this->widget('CCaptcha',array('captchaAction' => '/page/captcha',))?>

Thank you for reply!

This is right action. It’s a page_module/page_controller/captcha_action. If I leave only page/captcha the captcha img disappears.