YII 数据库,模型,登录验证
//protected/config/main.php //数据库连接设置 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=blog', 'emulatePrepare' => true,//PDO扩展 'username' => 'root', 'password' => 'xxxxxx', 'charset' => 'utf8', 'tablePrefix' => 'hd_', //定义表前缀, 'enableParamLogging' => TRUE //开启调试信息的SQL语句具体值信息 ),
这些属性可以到 framework/db/CDbConnection.php中查看
IndexController.php
<?php class IndexController extends Controller{ public function actionIndex(){ $LoginForm = new LoginForm(); if(isset($_POST['LoginForm'])){ $LoginForm->attributes = $_POST['LoginForm']; if($LoginForm->validate() && $LoginForm->login()){ exit('登陆成功,往下执行。。。。。'); } } $this->render('index',array('LoginForm'=>$LoginForm)); } /* * 数据库 测试 */ public function actionTest(){ $userInfo = Admin::model()->find('username = :name',array(':name'=>'admin')); dump($userInfo->password); } /* * 验证码 * index.php?r=admin/index/captcha 通过URL可以直接访问到图片 */ public function actions(){ return array( 'captcha'=>array( 'class'=>'system.web.widgets.captcha.CCaptchaAction', 'height' => 25, 'width' => 80, 'minLength' => 4, 'maxLength' => 4 ) ); } }
模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>后台首页</title> </head> <body> <h1><center>后台首页</center></h1> <?php $form = $this->beginWidget('CActiveForm') ?> 用户名:<?php echo $form->textField($LoginForm,'username',array('id'=>'username')); ?><br/> 密 码:<?php echo $form->passwordField($LoginForm,'password',array('id'=>'password')); ?><br/> 验证码:<?php echo $form->textField($LoginForm,'captcha',array('id'=>'verify')); ?> <?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer'))); ?> <br/> <input type="submit" value="登录"/> <?php $this->endWidget() ?> <div id="error"> <h4><center> <?php echo $form->error($LoginForm,'username') ?> </center></h4> <h4><center> <?php echo $form->error($LoginForm,'password') ?> </center></h4> <h4><center> <?php echo $form->error($LoginForm,'captcha') ?> </center></h4> </div> </body> </html>
LoginForm.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 LoginForm extends CFormModel { public $username; public $password; public $rememberMe; public $captcha; private $_identity; /** * Declares the validation rules. * The rules state that username and password are required, * and password needs to be authenticated. */ public function rules() { return array( // username and password are required array('username','required','message'=>'用户名不能为空'), array('password','required','message'=>'密码不能为空'), // rememberMe needs to be a boolean array('rememberMe', 'boolean'), // password needs to be authenticated array('password', 'authenticate'), array('captcha','captcha','message'=>'验证码错误') ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'rememberMe'=>'Remember me next time', ); } /** * 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('password','用户名或密码错误'); } } /** * 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,$duration); return true; } else return false; } }
Admin.php
class Admin extends CActiveRecord{ public static function model($className = __CLASS__){ return parent::model($className); } public function tableName(){ return "{{admin}}"; } }