Thursday, April 12, 2012

Make captcha in yii framework

If you want to make captcha in yiiframework you have gone to the right place. I want to teach you about how to make captcha in yiiframework.

You can make captcha in yiiframework easy because yii has featured to make the captchaa easily. So let't take a look how to make the captcha in yiiframework

To make the capctha you have to do :
  • Add the code rules to your controller id. For example UserController. 
  •  public function actions(){
      return array(
        // captcha action renders the CAPTCHA image displayed on the user registration page
        'captcha'=>array(
          'class'=>'CCaptchaAction',
          'backColor'=>0xFFFFFF,
        ),
      );
     }
    
  • The view form should like this
  • $form=$this->beginWidget('CActiveForm',array(
       'id'=>'user-form',
       'enableAjaxValidation'=>false,
     ));
     ?>
    Don't forget to put the access rules like this :
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
          'actions'=>array('create','update','captcha'),
          'users'=>array('@'),
        ),
    
  • Add field in your model for example User.php
  • public $verifyCode; 
    Add this code to your view form
    <?php if(extension_loaded('gd')): ?>
     <div class="row">
      <?php echo $form->labelEx($model,'verifyCode'); ?>
      <div>
       <?php $this->widget('CCaptcha'); ?>
       <?php echo $form->error($model,'verifyCode'); ?>
       <?php echo $form->textField($model,'verifyCode'); ?>
      </div>
    
     </div>
    <?php endif ?> 
  • The last add the rules here in the rules() model
  • array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
    
Oke that's all.. rock with yii framework

10 comments:

  1. Thank you very much..
    I follow code given step by step.

    I got Captcha now :-)

    ReplyDelete
  2. the code is very effective.....i finally got the captcha in my website.formore info visit my website

    captcha solver

    ReplyDelete
  3. Thank you very much! Your blog is really usefull.

    ReplyDelete
  4. wonderful!!!its a great job.your blog is very useful and informative.thanks for sharing.
    keep blogging.

    death by captcha

    ReplyDelete
  5. wow!!!
    the great blog.the blog is very interesting and very informative.
    thanks for sharing the information.

    captcha solver

    ReplyDelete
  6. Captcha image doesn't show. Any tips on troubleshooting this? I get the text field, and when I submit it tells me that capcha code is incorrect,but there is no capcha image. When I check the source, I can see the image is generated as < img id="yw0" src="/site/captcha?v=5212c61a3b9ea" alt="">.
    When I enter that URL into the browser, it tells me that the image can't be displayed because contains errors!!?? Any ideas?

    ReplyDelete
  7. Do you have follow all the instruction.. maybe in the rules..
    You have to make sure to have access to the captcha action..

    array('allow', // allow authenticated user to perform 'create' and 'update' actions
    'actions'=>array('create','update','captcha'),
    'users'=>array('@'),
    ),

    ReplyDelete
  8. I followed the instructions and I do have those rules setup:
    public function accessRules()
    {
    return array(
    array('allow',
    'actions'=>array('index','contact','view','create','captcha'),
    'users'=>array('*'),
    ),
    );
    }

    The problem is, even the contact page generated automatically by yiic when I started the application, stopped showing the captcha. I know the server is OK because it works on another project I have, but for this project doesn't work anywhere...
    I'm stuck. I tried to enable logging to find the error, but I get no error messages. I have 2 days now of searching with no success. I'm thinking to use GIT and revert back my project to the early stages to find out where it stopped working.

    ReplyDelete
  9. Hai, This is a superb Blog.. and nice informative scripts its new way to controller id further new idea for this..... they Rules and Usercontroller Script is really Superb and easy to useage... simple and super script framework is great finding... thanks for sharing..

    Decaptcha

    ReplyDelete
  10. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.Yiioverflow is a web development company.We have well expert team in Angular JS, Ionic, Yii Framework, Node JS, Laravel, PHP, MySQL, and WordPress.If you want a developer visit.. https://yiioverflow.com/

    ReplyDelete