忘记密码自定义忘记密码在yii

时间:2014-04-01 05:10:03

标签: yii

我是YII框架中的新手。我想添加"忘记密码"以及登录表单。当用户点击它时,系统将从数据库中获取该用户的电子邮件地址,并通过电子邮件向该用户发送新密码。我不想使用任何插件。

这是我的链接代码: -

        <?php echo CHtml::Link(Yii::t('job','Forgot Password ?'),
                                $this->createUrl('site/forgotpassword'),
                                array(
                                    'onclick'=>'$("#jobDialog").dialog("open"); return false;',
                                    'update'=>'#jobDialog'
                                ),
                                array('id'=>'showJobDialog'));?>
<div id="jobDialog"></div>

这是我的网站控制器: -

public function actionForgotPassword()
{
    $model  = new User;
    $this->renderPartial('createDialog',array('model'=>$model,),false,true);
}

这是我的createdialog.php: -

<?php 

$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
                'id'=>'jobDialog',
                'options'=>array(
                    'title'=>Yii::t('job','Fogot Password ?'),
                    'autoOpen'=>true,
                    'modal'=>'true',
                    'width'=>'auto',
                    'height'=>'auto',
                ),
                ));
echo $this->renderPartial('_formDialog', array('model'=>$model)); ?>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>

这是我的_formDialog.php

<div class="form" id="jobDialogForm">
 <style type="text/css">
.forgot_password{
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #DDDDDD;
    border-radius: 3px;
    font-family: Tahoma,Arial,Helvetica,sans-serif;
    font-size: 11px;
    padding: 5px 3px;
    width:70%;
}
label{
    color: #575757;
    display: block;
    font-size: 12px;
    font-weight: bold;
    margin: 0 0 5px;
    padding: 0 0 0 3px;
    }
</style>
<?php $form=$this->beginWidget('CActiveForm', array(
                                            'id'=>'job-form',
                                            'enableAjaxValidation'=>true,
                                )); 
//I have enableAjaxValidation set to true so i can validate on the fly the
?>
    <p class="note">Fields with <span class="required">*</span> are required.</p>
    <!---flash message start------>
<?php if(Yii::app()->user->hasFlash('error')):?>
        <div class="alert alert-error">
                <?php echo Yii::app()->user->getFlash('error'); ?>
        </div>
<?php endif; ?>
<!----flash message end----->
    <?php echo $form->errorSummary($model); ?>
    <div class="row">
        <?php echo $form->labelEx($model,'email'); ?>
        <?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>180,'class'=>'forgot_password')); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::ajaxSubmitButton(Yii::t('job','Submit'),CHtml::normalizeUrl(array('site/forgotpassword1','render'=>false)),array('success'=>'js: function(data) {
                        $("#jobDialog").dialog("close");
                    }'),array('id'=>'closeJobDialog','onclick'=>'return forgotpassword()')); ?>
    </div>

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

</div>
<script type="text/javascript">
function forgotpassword()
    {
        var email           = $("#User_email").val();
        if(email=='')
        { 
            $("#errorEmail").remove();
             $('#User_email').after('<div class="errorMessage" id="errorEmail">Email should not be blank.</div>');
             return false;
        }
        else
        {
            $("#errorEmail").remove();
        }

    }
</script>

1 个答案:

答案 0 :(得分:1)

将它放在你的控制器中,

private function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

public function actionForgotPassword($email) {
    $user = User::model()->findAll('email=:email',
                array('email'=>$email);

    if(count($user) != 1) {
        Yii::app()->user->setFlash('user',
            Yii::t('messages', 'Unable to find user.'));
        );
        $this->refresh();
    }
    else {
        $user = $user[0];
        $user->password = $this->randomPassword();
        $user->update();

        // Send new password to email
        $from = 'Password Reset <admin@yoursite.com>';
        $to = $user->email;
        $name = $user->firstname.' '.$user->lastname;
        $subject = 'Reset Password';

        $message = Yii::t('user', 'Dear').' '.$user->username.',
'.Yii::t('user', 'A request has been made to reset your password.').'
'.Yii::t('user', 'Your new password is').': '.$user->password.'
'; // Our message above

        $headers = 'From: '.$from."\r\n"; // Set from headers
        mail($to, $subject, $message, $headers); // Send our email

        Yii::app()->user->setFlash('user',
            Yii::t('notices', 'A new password has been sent to your email address.')
        );
        $this->refresh();
    }

    $this->layout = '//layouts/main';
    $this->render('resetpassword');
}

同时添加一个视图&#34; resetpassword.php&#34;:

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