Facing difficulties to activate user registration by email

[size="7"][/size]Hi All,

I am facing problem to activate an user by click on activation link that sent by email.

Here is my code-

UserController.php:


public function actionCreate()

	{

		$model=new User;

		// Uncomment the following line if AJAX validation is needed

		 $this->performAjaxValidation($model);

    

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

		{

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

                    $model->createtime=date('Y-m-d H:i:s');

      

                    $model->activationcode = sha1(mt_rand(10000, 99999).time().$model->email);

                     


		     if($model->save())

                      { 

                        $to = $model->email;

                        $subject = "Welcome To My Site!"         


                        $message2 = "<html>

                                      <body>Please click this below to activate your membership<br />".

                                       Yii::app()->createAbsoluteUrl('user/check', array('activationcode' => $model->activationcode))."           

                            Thanks you

                            

                          </body>

                        </html>";

              


          $headers  = 'MIME-Version: 1.0' . "\r\n";

          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

          $headers .= 'From: My Site < admin@mysite.com>' . "\r\n";

                             

          mail($to,$subject,$message2,$headers);

              

          $this->redirect(array('view','id'=>$model->id));

      }

    }

   


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


	}

UserController.php:


public function actionCheck()

  {

    $activationcode = Yii::app()->request->getQuery('activationcode');

    // collect user input data

    if(isset($activationcode))

    {

         

      $model = User::model()->find('activationcode=:activationcode', array(':activationcode'=>$activationcode));

      

      if($activationcode == $model->activationcode)

      {            

        $model->status=1;

        //$model->validate();

        $model->save();

        Yii::app()->user->setFlash('check','Thank you for register with us');

        $this->refresh();

      }

            

    }


    // display the login form

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


  }

View file check.php:


<?php if(Yii::app()->user->hasFlash('check')): ?>


<div class="flash-success">

	<?php echo Yii::app()->user->getFlash('check'); ?>

</div>


<?php endif; ?>

After click on activation link then status is not changed to 1. I need quick help.

With thanks,

MRS

Can you post your model rules?

Also, you have a bug, in that if the code is not found in the database, $model will be null. Trying to access $model->activationcode will give you an error.

You could restructure the code like this:




    if(isset($activationcode))

    {

      $model = User::model()->findByAttributes(array('activationcode'=>$activationcode));

      

      if($model !== null)

      {            

        $model->status=1;

        $model->save();

        Yii::app()->user->setFlash('check','Thank you for register with us');

        $this->refresh();

      }

    }



Specifically, you should be comparing the returned model to null to determine if the code was found.

Thanks for quick reply.

Here is my model rules:


public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('fullname, email, password, verifyPassword', 'required'),

      array('email', 'unique'),

      array('email', 'email'),

      array('verifyPassword', 'compare', 'compareAttribute'=>'password'),

      array('password', 'length', 'min'=>3),

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

			array('organisation', 'length', 'max'=>100),

			array('telephone, fullname', 'length', 'max'=>50),

			array('email, password, activationcode', 'length', 'max'=>128),

			array('id, organisation, telephone, fullname, email, password, activationcode, createtime, lastvisit, superuser, status', 'safe', 'on'=>'search'),

		);

	}

I hope it will help you to understand my problem.

Thanks,

MRS

All you need to do is declare that ‘Status’ is a ‘safe attribute’ in order to save it. In your model,




array('status', 'safe')



Read more @ http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/

Hi nguyendh,

I have tested your code but its not working. What will be the right solution?

Regards,

MRS

You need to determine which part of the process is failing. Place temporary echo or var_dump statements at various points in the code to determine which part of the code is not behaving in the way that you expect.

It’s difficult for anyone here to help you until you’ve narrowed down the location of the bug.

Your code should work. I recommend to do CVarDumper::dump($model->getErrors(), 10, true) after you validate your $model to see if there are any validation errors. Also, dump your $model before you save it to make sure the status flag is set.

Hi,

After click on activation link http://www.example.com/devtest/index.php?r=user/check&activationcode=bc74873d0e3f684d3e6b99a36169a793ee688406 then it redirect to login page. I think my following controller code not working for view file check.php which located on user directory.


public function actionCheck()

  {

    $model=new User;

    $activationcode = Yii::app()->request->getQuery('activationcode');

    

    if(isset($activationcode))

    {

      $model = User::model()->findByAttributes(array('activationcode'=>$activationcode));

      

      if($model !== null)

      {            

        $model->status=1;

        $model->save();

        Yii::app()->user->setFlash('check','Thank you for register with us');

        $this->refresh();

      }

      

    }


    $this->render('check',array(

			'model'=>$model,

		));


  }

I am not sure how I can handle GET URL action in UserController. Also, I already tested by adding word ‘check’ in accessRules but then browser show me Page not redirect properly.


public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

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

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

			),

.....

.....

);

}

Any idea? Please give me a solution.

With Thanks,

MRS