Yii Framework Forum: How to activate user registration by email? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to activate user registration by email? Rate Topic: -----

#1 User is offline   gezzeg 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-December 10

Posted 28 March 2012 - 01:59 AM

Hi,

I try google about how to activate user registration by email but didnt find the answer.

Any one could share tips on how to do it in Yii?
0

#2 User is offline   Mukesh 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 17-May 11
  • Location:India

Posted 28 March 2012 - 02:07 AM

The way i do it -

1. Generate a random sha1 key using the user's email -
$model->activation_key = sha1(mt_rand(10000, 99999).time().$model->email);

2. Store that in the database along with other user details and the activation status as 'false' or 0.
3. Construct a url which points to another action within the controller and has the activation key as a GET parameter, which will verify the activation key by comparing with the keys in the database. Send that url in an email to the user.
$activation_url = $this->createAbsoluteUrl('registration/validateKey', array('key'=>$model->activation_key));

4. In the activation key validation action, compare the key in the get parameter against the keys in the database. If match found and activation status = 0, change activation status to 1 and tell user that everything is now hunky dory ;)

That's it.
3

#3 User is offline   gezzeg 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-December 10

Posted 28 March 2012 - 02:10 AM

View PostMukesh, on 28 March 2012 - 02:07 AM, said:

The way i do it -

1. Generate a random sha1 key using the user's email - sha1(mt_rand(10000, 99999).time().$model->email);
2. Store that in the database along with other user details and the activation status as 'false' or 0.
3. Construct a url which points to another action within the controller and has the activation key as a GET parameter, which will verify the activation key by comparing with the keys in the database. Send that url in an email to the user.
4. In the activation key validation action, compare the key in the get parameter against the keys in the database. If match found and activation status = 0, change activation status to 1 and tell user that everything is now hunky dory ;)

That's it.


Wah so fast! Thanks Mukesh! I will try it right away!
0

#4 User is offline   gezzeg 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-December 10

Posted 30 March 2012 - 10:06 AM

View Postgezzeg, on 28 March 2012 - 02:10 AM, said:

Wah so fast! Thanks Mukesh! I will try it right away!


i already got it. Can you comment any add on or weakness? Sorry yii newb here.

i make actionRegister() in SiteController.php

  public function actionRegister() {
                   
                    $model=new RegisterForm;
                    $newUser = new User;
                    
                    // if it is ajax validation request
                    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
                    {
                            echo CActiveForm::validate($model);
                            Yii::app()->end();
                    }
    
                    // collect user input data
                    if(isset($_POST['RegisterForm']))
                    {
                            $model->attributes=$_POST['RegisterForm'];
                            
                         //   if ($model->validate()) {
                                    $newUser->username = $model->username;
                                    //$newSalt = $newUser->generateSalt();
                                    $newSalt=$newUser->generateSalt(); 
                                    
                                    $newUser->password = $newUser->hashPassword($model->password,$newSalt);
                                    $newUser->salt = $newSalt;
                                    
                                    $newUser->activationcode = $newUser->generateActivationCode($model->email);
                                  // $newUser->activationcode = sha1(mt_rand(10000, 99999).time().$email);
                                   $newUser->activationstatus = 0;
    
                                    //return $this->hashPassword($password,$this->salt)===$this->password;
                                    $newUser->username = $model->username;
    
                                    $newUser->email = $model->email;
                                    $newUser->joined = date('Y-m-d');
                                            
                                    if ($model->validate() && $newUser->save()) {
                                           //if want to go login, just uncomment this below
                                           // $identity=new UserIdentity($newUser->username,$model->password);
                                           // $identity->authenticate();
                                           // Yii::app()->user->login($identity,0);
                                            //redirect the user to page he/she came from
                                            
                                            //email activation code starts-----------------------------------------
                                           
                                            $to = $model->email;
                                            $subject = "Welcome To GhazaliTajuddin.com!";
                                            $message = "Thank you for joining!, we have sent you a separate email that contains your activation link";
                                            $from = "FROM: mr.ghazali@gmail.com";
                                            
                                            mail($to,$subject,$message,$from);
                                            
                                            //echo $to.$subject.$message.$from;
                                            
                                           $headers  = 'MIME-Version: 1.0' . "\r\n";
                                           $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
                                           $headers .= 'From: Mr. Ghazali < mr.ghazali@gmail.com>' . "\r\n";
                                            
                                           $subject2 = "Your Activation Link";
    
                                           $message2 = "<html><body>Please click this below to activate your membership<br />".
                                                                    Yii::app()->createAbsoluteUrl('site/activate', array('email' => $newUser->email)).
                                                                    
                                                                   "
                                                                   
                                                                   Thanks you.
                                                                   ". sha1(mt_rand(10000, 99999).time().$email) ."
                                                                   </body></html>";
                                                                    
                                          mail($to, $subject2, $message2, $headers);
                                            //email activation code end-----------------------------------------
                                            
                                            $this->redirect(Yii::app()->user->returnUrl);
                                            
                                    }
                          //  }
                                    
                    }
                    // display the register form
                    $this->render('register',array('model'=>$model));


I also create actionActivate() in SiteController.php

    	
/**
    	 * Activation Action
    	 */
    	public function actionActivate()
    	{
     
            $email = Yii::app()->request->getQuery('email');
            
    		// collect user input data
    		if(isset($email))
    		{
    		  
              $model = User::model()->find('email=:email', array(':email'=>$email));   
              
              if($email == $model->email){            
                $model->activationstatus=1;
                $model->validate();
                $model->save();
              }
                        
    		}
      
    		// display the login form
    		 $this->render('activate',array('model'=>$model));
    
    	}

0

#5 User is offline   rhomb 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 28-September 11

Posted 31 March 2012 - 06:57 AM

I'm just gonna answer here real quick if you don't mind.

Because it's sth you most likely will use more often I would sugggest to build a Mail Component extending CModel.

That way you can just do sth around the lines of:

$mail = new Mail;
$mail->to = $model->email;
$mail->body = $this->renderPartial('_mailTemplate', compact('model'), true);
if (!$mail->send())
    throw new CHttpException(500, 'something went horrible wrong');
else
    // set flash if needed here
    $this->redirect('site/index');



That would slim down your registerAction by quite a bit, and follows DRY.
Also the Mail Sending happens in the Component (either with normal mailsend or smtp with swiftMailer)
Thanks to inheriting from CModel you can use validation rules like in any other model.
Mail->send() pretty much just runs Mail->validate and send mail out afterwards.

example for actionActivate:
public funciton actionActivate($activation) {
    $model= User::model()->findByAttributes(array(
      'activation_code' => $activation
    ));
    if ($model === null)
       throw new CHttpException(404, 'Not found');
    else 
       $model->activationstatus = 1;
    $model->save();
    //redirect / flash / login whatever
}



just my two cents to get some more structure in the code.. yours will most likely work, but be a pain to maintain over time.

rho
0

#6 User is offline   gezzeg 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-December 10

Posted 02 April 2012 - 11:35 AM

View Postrhomb, on 31 March 2012 - 06:57 AM, said:

I'm just gonna answer here real quick if you don't mind.

Because it's sth you most likely will use more often I would sugggest to build a Mail Component extending CModel.

That way you can just do sth around the lines of:

$mail = new Mail;
$mail->to = $model->email;
$mail->body = $this->renderPartial('_mailTemplate', compact('model'), true);
if (!$mail->send())
    throw new CHttpException(500, 'something went horrible wrong');
else
    // set flash if needed here
    $this->redirect('site/index');



That would slim down your registerAction by quite a bit, and follows DRY.
Also the Mail Sending happens in the Component (either with normal mailsend or smtp with swiftMailer)
Thanks to inheriting from CModel you can use validation rules like in any other model.
Mail->send() pretty much just runs Mail->validate and send mail out afterwards.

example for actionActivate:
public funciton actionActivate($activation) {
    $model= User::model()->findByAttributes(array(
      'activation_code' => $activation
    ));
    if ($model === null)
       throw new CHttpException(404, 'Not found');
    else 
       $model->activationstatus = 1;
    $model->save();
    //redirect / flash / login whatever
}



just my two cents to get some more structure in the code.. yours will most likely work, but be a pain to maintain over time.

rho


Thanks for your suggestion. Will looking forward on creating mail component.
0

#7 User is offline   Katio 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 09-May 12

Posted 22 May 2012 - 02:55 PM

Thank you Mukesh and friends. That's exactly what I was looking for.
0

#8 User is offline   mrs 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 05-September 12
  • Location:Bangladesh

Posted 08 September 2012 - 06:10 AM

Hi,
After click on email activation link http://www.example.c...169a793ee688406 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.
You will find complete discussion in http://www.yiiframew...538#entry169538

With Thanks,
MRS
Explore and Enjoy Yii 2...
0

#9 User is offline   vinoth.mohan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 08-June 13
  • Location:Malaysia

Posted 10 June 2013 - 04:15 AM

Hi ter,

Im also looking for same solution. got any working solution.
pls share it


thank you.
0

#10 User is offline   kumarkulandai 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 18-November 11
  • Location:Bangalore

Posted 10 June 2013 - 05:31 AM

for each users maintain one status column like 'active' in Table, default value for this column is inactive.

when user clicks the link from mail/invitation, activate the user by updating the value from inactive to active?
Regards,
Kumar Kulandai.

Posted Image
0

#11 User is offline   Peter Hayman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 61
  • Joined: 11-June 12
  • Location:Wake Forest

Posted 10 June 2013 - 06:20 AM

Not sure if you are running into this, but I had to set up my urlManager in config/main.php like this:

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


FYI... this caused a infinite loopback when using Gii. I think there is a fix for this, but I just comment it out on the rare times I use Gii in an up and running application.
0

#12 User is offline   kumarkulandai 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 18-November 11
  • Location:Bangalore

Posted 10 June 2013 - 06:36 AM

try this config for URL manager

'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>',
),
)
),
Regards,
Kumar Kulandai.

Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users