警匪
- 警匪 回复了话题 [分享] 自己做了会员注册登录,贡献出来
- components/UserIdentity.php 代码:
<?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */ class UserIdentity extends CUserIdentity { private $_id; private $_email; const ERROR_EMAIL_INVALID = 1; /** * Authenticates a user. * The example implementation makes sure if the username and password * are both 'demo'. * In practical applications, this should be changed to authenticate * against some persistent user identity storage (e.g. database). * @return boolean whether authentication succeeds. */ public function authenticate() { $email = strtolower($this->username); $user = User::model()->find('LOWER(email)=?', array($email)); if($user === null) { $this->errorCode = self::ERROR_EMAIL_INVALID; } else if(!$user->validatePassword($this->password)){ $this->errorCode = self::ERROR_PASSWORD_INVALID; } else { $this->_id = $user->user_id; $this->_email = $user->email; $this->errorCode = self::ERROR_NONE; } return $this->errorCode; } // Get User ID public function getId() { return $this->_id; } public function getEmail() { return $this->_email; } }
- views/user/register.php
<?php $this->pageTitle = Yii::app()->name . ' - 用户注册'; $this->breadcrumbs = array('用户注册') ?> <h1>注册</h1> <div class="form"> <?php echo CHtml::beginForm(); ?> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'email'); ?> <?php echo CHtml::activeTextField($model, 'email', array('size' => 30, 'maxlength' => 100)); ?> <?php echo CHtml::error($model, 'email'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'password'); ?> <?php echo CHtml::activePasswordField($model, 'password', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'password'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'passwordConfirm'); ?> <?php echo CHtml::activePasswordField($model, 'passwordConfirm', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'passwordConfirm'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'nick_name'); ?> <?php echo CHtml::activeTextField($model, 'nick_name', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'nick_name'); ?> </div> <div class="row"> <?php $this->widget('CCaptcha'); ?> <?php echo CHtml::activeLabelEx($model, 'verifyCode'); ?> <?php echo CHtml::activeTextField($model, 'verifyCode', array('size' => 8, 'maxlength' => 10))?> </div> <div class="row"> <?php echo CHtml::submitButton('注册', array('name' => 'submit'))?> </div> <?php echo CHtml::endForm(); ?> </div>
- views/user/login.php
<?php $this->pageTitle=Yii::app()->name . ' - Login'; $this->breadcrumbs=array( 'Login', ); ?> <h1>Login</h1> <p>Please fill out the following form with your login credentials:</p> <div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'login-form', 'enableClientValidation'=>true, 'clientOptions'=>array( 'validateOnSubmit'=>true, ), )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <div class="row"> <?php echo $form->labelEx($model,'email'); ?> <?php echo $form->textField($model,'email'); ?> <?php echo $form->error($model,'email'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'password'); ?> <?php echo $form->passwordField($model, 'password'); ?> <?php echo $form->error($model, 'password'); ?> <p class="hint"> Hint: You may login with <tt>demo/demo</tt> or <tt>admin/admin</tt>. </p> </div> <div class="row"> <?php $this->widget('CCaptcha'); ?> <?php echo $form->textField($model, 'verifyCode'); ?> <?php echo $form->error($model, 'verifyCode'); ?> <p class="hint"> Hint: You may login with <tt>demo/demo</tt> or <tt>admin/admin</tt>. </p> </div> <div class="row rememberMe"> <?php echo $form->checkBox($model, 'remember'); ?> <?php echo $form->label($model, 'remember'); ?> <?php echo $form->error($model, 'remember'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Login'); ?> <?php echo CHTML::link('注册', array('user/register'))?> </div> <?php $this->endWidget(); ?> </div><!-- form -->
- 警匪 发布了话题[分享] 自己做了会员注册登录,贡献出来
问题解决了,我把 beforSave 方法定义成了 public,改成这样就好了
protected function beforeSave() { if($this->isNewRecord) { $this->salt = $this->verifyCode; $this->password = md5($this->salt . $this->password); $this->create = time(); } return parent::beforeSave(); }
还没弄清,先头生成个form,没生成出来,就没搞
舰长啊。我发了。。。
这是视图
<div class="form"> <?php echo CHtml::beginForm(); ?> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'email'); ?> <?php echo CHtml::activeTextField($model, 'email', array('size' => 30, 'maxlength' => 100)); ?> <?php echo CHtml::error($model, 'email'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'password'); ?> <?php echo CHtml::activePasswordField($model, 'password', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'password'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'passwordConfirm'); ?> <?php echo CHtml::activePasswordField($model, 'passwordConfirm', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'passwordConfirm'); ?> </div> <div class="row"> <?php echo CHtml::activeLabelEx($model, 'nick_name'); ?> <?php echo CHtml::activeTextField($model, 'nick_name', array('size' => 30, 'maxlength' => 100))?> <?php echo CHtml::error($model, 'nick_name'); ?> </div> <div class="row"> <?php $this->widget('CCaptcha'); ?> <?php echo CHtml::activeLabelEx($model, 'verifyCode'); ?> <?php echo CHtml::activeTextField($model, 'verifyCode', array('size' => 8, 'maxlength' => 10))?> </div> <div class="row"> <?php echo CHtml::submitButton('注册', array('name' => 'submit'))?> </div> <?php echo CHtml::endForm(); ?> </div>
- 警匪 发布了话题新手,会员注册登录问题