//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}}";
}
}

YII 数据库,模型,登录验证的更多相关文章

  1. MVC5+EF6入门教程——实现动态创建数据库与登录验证

    详细步骤 创建文件夹,规划好项目目录 创建相关实体类 (Data Model) 创建 Database Context 创建Initializer, 使用EF初始化数据库,插入测试数据 实现数据库登录 ...

  2. 3月9日(用 DBHelper 工具连接 mysql 数据库 实现登录验证)

    一. 用DBHelper 与mysql 连接 实现最简单的登录验证. (1)新建 web project ----->选择src导入 DBHelper 工具包-------->选择web  ...

  3. asp.net MVC中如何用Membership类和自定义的数据库进行登录验证

    asp.net MVC 内置的membershipProvider可以实现用户登陆验证,但是它用的是自动创建的数据库,所以你想用本地数据库数据去验证,是通过不了的. 如果我们想用自己的数据库的话,可以 ...

  4. HTML+CSS登录界面,有数据库的登录验证

    HTML 1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <head> 4 <meta charset ...

  5. Django学习系列之重写User模型和登录验证

    重写User模型 Django内置的User模型可能不适合某些项目,我们可能要基于内置的添加一些字段 创建users app startapp users 修改settings.py配置文件,覆盖默认 ...

  6. java桥连接sql server之登录验证及对数据库增删改查

    一:步骤 1.sql server建立数据库和相关表 2.建立数据源  (1).打开控制面板找到管理,打开ODBC选项或直接搜索数据源  (2).打开数据源配置后点击添加,选择sql server点击 ...

  7. 采用集成的Windows验证和使用Sql Server身份验证进行数据库的登录

    采用集成的Windows验证和使用Sql Server身份验证进行数据库的登录 1.集成的Windows身份验证语法范例 1 string constr = "server=.;databa ...

  8. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  9. Django 博客项目01 数据库设计与验证码校验+Ajax登录

    数据库设计 from django.db import models from django.contrib.auth.models import AbstractUser class UserInf ...

随机推荐

  1. g1gc

    http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All http://blog.csdn.net/renfufe ...

  2. elisp

    cons cell? 构建内存对象! 通过寄存器的基地址car和偏移地址cdr来--寻址内存对象,我是这样理解的. http://wiki.dourok.info/doku.php/%E5%B7%A5 ...

  3. windows 系统注册dll文件

    使用管理员身份注册:命令提示符 管理员身份运行 32 位系统:regsvr32 %windir%\system32\jscript.dll 64 位系统:regsvr32 %windir%\SysWO ...

  4. 解决MyEclipse吃内存以及卡死的方法 (转)

    前言:MyEclipse5.5 大小 139M:MyEclipse6.5 大小 451M:MyEclipse7.0 大小 649M!下载服务器又是国外的...下载速度累人也就罢了,只要你工作性能一流. ...

  5. Mercurial hg web server的配置

    在windows下安装tortoisehg-1.0.3-hg-1.5.3-x64.exe的版本控制工具后,克隆建立中心库后,启动web server,其他分库可以连接中心库进行pull但无法push. ...

  6. HTML5的兼容问题以及调用js文件的方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Type safety: Unchecked cast from Object to ArrayList

    表明Object转化为ArrayList这个转化并不是安全的.. 编译的时候需要加入修饰符才能正常编译(具体是那个修饰符..不记得了.^_^),否则会提示有警告 当然这只是一个警告,如果楼主自信这个转 ...

  8. 有关sqlitedrop数据库重建比delete方式来清空数据库更加有效率

    今天浏览stackoverflow 发现一个有趣的问题: which was more preferable as performance wise and without error cause t ...

  9. perties类的操作

    http://www.cnblogs.com/bakari/p/3562244.html perties类的操作   知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源 ...

  10. 一个UWSGI的例子

    摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...