How To : Create Forgot Password in Yii (Sending Email to Reset Password)

  1. eg : siteController.php
  2. create 2 file name : forgot.php , verifikasi.php
  3. for forgot.php :
  4. for verifikasi.php :

Assalamualaikum wr.wb Hello yii people i'm Muhammad Fahmi Azain this is tutorial about forgot password in yii framework, and i'm sorry if it does not comply with code standards yii. okay let's begin

before we discuss this tutorial, please create table users with this structure and don't forget to creating your Users model.

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `nama_asli` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `token` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

in your controller and create some action or you can look at below :

eg : siteController.php

public function getToken($token)
	{
		$model=Users::model()->findByAttributes(array('token'=>$token));
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
        

        public function actionVerToken($token)
        {
            $model=$this->getToken($token);
            if(isset($_POST['Ganti']))
            {
                if($model->token==$_POST['Ganti']['tokenhid']){
                    $model->password=md5($_POST['Ganti']['password']);
                    $model->token="null";
                    $model->save();
                    Yii::app()->user->setFlash('ganti','<b>Password has been successfully changed! please login</b>');
                    $this->redirect('?r=site/login');
                    $this->refresh();
                }
            }
            $this->render('verifikasi',array(
			'model'=>$model,
		));
        }

        
        public function actionForgot()
	{
            $getEmail=$_POST['Lupa']['email'];
            $getModel= Users::model()->findByAttributes(array('email'=>$getEmail));
            if(isset($_POST['Lupa']))
            {
                $getToken=rand(0, 99999);
                $getTime=date("H:i:s");
                $getModel->token=md5($getToken.$getTime);
                $namaPengirim="Owner Jsource Indonesia";
                $emailadmin="fahmi.j@programmer.net";
                $subjek="Reset Password";
                $setpesan="you have successfully reset your password<br/>
                    <a href='http://yourdomain.com/index.php?r=site/vertoken/view&token=".$getModel->token."'>Click Here to Reset Password</a>";
                if($getModel->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($namaPengirim).'?=';
				$subject='=?UTF-8?B?'.base64_encode($subjek).'?=';
				$headers="From: $name <{$emailadmin}>\r\n".
					"Reply-To: {$emailadmin}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-type: text/html; charset=UTF-8";
				$getModel->save();
                                Yii::app()->user->setFlash('forgot','link to reset your password has been sent to your email');
				mail($getEmail,$subject,$setpesan,$headers);
				$this->refresh();
			}
                
            }
		$this->render('forgot');
	}

in your views please create some file or you can look at below :

create 2 file name : forgot.php , verifikasi.php

and paste this code:

for forgot.php :

<?php
$this->pageTitle=Yii::app()->name . ' - Forgot Password';
$this->breadcrumbs=array(
	'Forgot Password',
);
?>
<?php if(Yii::app()->user->hasFlash('forgot')): ?>

<div class="flash-success">
	<?php echo Yii::app()->user->getFlash('forgot'); ?>
</div>

<?php else: ?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'forgot-form',
        'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>

	<div class="row">
            Email : <input name="Lupa[email]" id="ContactForm_email" type="email">
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Submit'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<?php endif; ?>

for verifikasi.php :

<?php
$this->pageTitle=Yii::app()->name . ' - Change Password';
$this->breadcrumbs=array(
	'Change Password',
);
?>
<h2>Hi! <?php echo $model->nama_asli;?> :v</h2>
<div class="form">
    <h2>Change Password</h2>
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'Ganti-form',
)); ?>

	<div class="row">
            New Password : <input name="Ganti[password]" id="ContactForm_email" type="password">
            <input name="Ganti[tokenhid]" id="ContactForm_email" type="hidden" value="<?php echo $model->token?>">
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Submit'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

Demo : demo forgot password Source Code : Download Source Code (Available)

sorry for my bad English okay well done... Alhamdulillah so.. it's easy and simple :D

more tutorials visit my blog : Fahmi Azain Blog 1 or Fahmi Azain Blog 2

Find Me on Github : https://github.com/ruderbytes

0 0
2 followers
Viewed: 65 377 times
Version: 1.1
Category: How-tos
Written by: ruderbytes
Last updated by: ruderbytes
Created on: Aug 21, 2014
Last updated: 8 years ago
Update Article

Revisions

View all history

Related Articles