//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. nfs安装配置

    一.NFS服务简介 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操 ...

  2. 2016沈阳网络赛 odd-even number

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. CentOS 单用户登录&命令行、图像界面

    如何单用户登录: 这是一个很简单的问题,以前没重视,每次linux服务器无法正常启动时,都找应急盘,想偷懒,反而浪费了时间. 今天备忘如下: 1.系统启动时,按光标键调出GRUB引导菜单. 2.选定一 ...

  4. Android--->activity界面跳转,以及查看生命周期过程

    main.xml界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  5. java source map

    Chrome 更新后出现了 jquery.min.map 404  (Not Found) 的信息 这个到底是什么东西?查询了一下,得到了以下资料 JQuery 官方解释 摘录一下內容 从 jQuer ...

  6. IE11 Windows7下F12 DOC资源管理器不能用Exception in window.onload: Error: An error has ocurredJSPlugin.3005

    ------------------ Diagnostic: Exception in window.onload: Error: An error has ocurredJSPlugin.3005 ...

  7. HTTP Request header

    HTTP Request header HTTP协议详解 - 小坦克 - 博客园 HTTP Request header 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Pe ...

  8. 高效判断奇偶性,利用位运算符&

    这种位运算判断奇偶性,在程序和数据库里面都是可以用的 public static bool isOdd(i) { return (i&1)!=0 } 最小奇数是:1   最小偶数是:0   所 ...

  9. memcached 第二篇----安装使用

    摘要:set add replace get delete gets cas stats 和 flush_all 命令 获取所有key  .你可以使用MemCachedClient的statsItem ...

  10. NSURL 子串截取

    NSURL *url = [NSURL URLWithString:@"http://reg.email.163.com/unireg/call.do?cmd=register.entran ...