yii不愧是模仿ruby on rails、因为以前用过rails、所以感觉yii的一些思想跟rails特别像、第一次用yii确实有点苦逼、留下自己代码、
用户注册controller UserController.php

//注册
	public function actionRegister(){
		 $user=new register();
        if(isset($_POST['register'])){  
            $user->attributes=$_POST['register'];
            if($user->save()){  
                $this->redirect(array('user/login'));
            }else{  
                echo 'error';  
            }  
        }
		$this->render('register',array("model"=>$user));
	}

用户注册model register.php

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class register extends CActiveRecord
{


	//多数据操作  caoke数据库
	protected $dbString='caoke';
	public function getDbConnection()
	{
			if(self::$db!==null)
					return self::$db;
			else
			{
					$dbString=$this->dbString;
					self::$db=Yii::app()->$dbString;
					if(self::$db instanceof CDbConnection)
					{
							self::$db->setActive(true);
							return self::$db;
					}
					else
							throw new CDbException(Yii::t('yii','...'));
			}
	}
	public $_pwd2;
	public $yzm;
	//public $rememberMe;

	private $_identity;
	//返回当前数据表的名字  
		public function tableName() {
			return 'users';//这样写可以省略前缀  
		}  
      
	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			array('email', 'required','message'=>'邮箱不能为空'),
			array('_pwd','required','message'=>'密码不能为空'),
			array('_pwd2','compare','compareAttribute'=>'_pwd','message'=>'两次密码不一致'),  
            array('email','email','allowEmpty'=>false,'message'=>'邮箱格式不正确'),
			array('email', 'unique','message'=>'该邮箱已注册'), // 唯一,不能有重复记录
			array('yzm', 'captcha', 'allowEmpty'=>!Yii::app()->user->isGuest,'message'=>'验证码错误'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => '密码:',
			'passwrod2'=>'重复密码:',
			'email' => 'Email',
			'yzm' =>'验证码:',
		);
	}

	
	//入库前操作
	
	protected function beforeSave() {
		if($this->isNewRecord) {
			$this->_pwd = md5($this->_pwd);
		}
		return parent::beforeSave();
		}
	
	
	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate())
				$this->addError('password1','邮箱或密码错误');
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			//$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity);
			return true;
		}
		else
			return false;
	}
}

view register.php

	<div id="space">
	<?php  
	//使用小物件生成form元素  
	$form=$this->beginWidget('CActiveForm');  
	?>
		<div class="log_pad">

			<div class="space20">
			<?php 
			if($form->error($model,'email')!=''){
				echo $form->error($model,'email');
			}elseif($form->error($model,'_pwd') != ''){
				echo $form->error($model,'_pwd');
			}elseif($form->error($model,'_pwd2') !=''){
				echo $form->error($model,'_pwd2');
			}elseif($form->error($model,'yzm') !=''){
				echo $form->error($model,'yzm');
			}
			?>
			</div>

			<table border="0" cellpadding="0" cellspacing="0" width="100%">

				<tr>

					<td align="center" colspan="2">用户注册</td>

				</tr>

				<tr>

					<td width="30%" align="right"><?php echo $form->labelEx($model,'Email:');?></td>

					<td width="70%">
						<?php echo $form->textField($model,'email',array('id'=>'memail'));?> 						
					</td>

				</tr>

				<tr>

					<td width="30%" align="right"><?php echo $form->labelEx($model,'password');?></td>

					<td width="70%">
					<?php echo $form->passwordField($model,'_pwd',array('id'=>'mpassword'));?>
					</td>

				</tr>

				<tr>

					<td width="30%" align="right"><?php echo $form->labelEx($model,'passwrod2');?></td>

					<td width="70%">
						<?php echo $form->passwordField($model,'_pwd2',array('id'=>'mpassword2'));?>
					</td>

				</tr>

				<tr>

					<td width="30%" align="right">
					<?php echo $form->labelEx($model,'yzm');?>
					</td>

					<td width="70%">
						<?php echo $form->textField($model,'yzm',array('id'=>'vaildator'));?>
						
						
					</td>

				</tr>

				<tr>

					<td width="30%" align="right"></td>

					<td width="70%">

						<?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?>

					</td>

				</tr>

				

				<tr>

					<td width="30%" align="right"></td>

					<td width="70%"><div id="msubmit"  onclick="$('#yw0').submit();">注册</div></td>

				</tr>

			</table>

			<a href="<?php echo CHtml::normalizeUrl(array('user/login')); ?>" data-ajax="false" class="go_reg">登录</a>

		</div>

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

最后的效果大概是这个样子、写的时候多注意model、model有一些无用代码、是gii生成的、可以忽略。

本文为松林原创,转载请注明出处,尊重他人成果。
http://www.songlin51.com/archives/1058.html