//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. 怎么把一个int数组转化为char型数组??

    /* 234 Press any key to continue */ #include <stdio.h> int main() { ,n; ]; ; num; ++n) { s[n] ...

  2. POJ 2296 Map Labeler

    二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...

  3. php登录

    if ($name && $passowrd){ $sql = "SELECT * FROM liuyanban WHERE name = '$name' and passw ...

  4. HDU 2121 Ice_cream’s world II 最小树形图 模板

    开始学习最小树形图,模板题. Ice_cream’s world II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  5. ibatis 学习笔记 3 - pfpfpfpfpf的专栏 - 博客频道 - CSDN.NET

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  6. js动画(三)

    咳咳咳咳,感冒了感冒了,鼻塞,蓝瘦啊!嘴巴也开裂,哎,心疼自己.想到这是第三只唇膏了!只怪,放荡不倔爱自由, 行驶在冷风路上么,北风那个吹啊吹啊吹啊,好了,发神经发完了,接下来进入正题,严肃脸.(字数 ...

  7. Spring MVC之Action返回类型

    Spring MVC支持的方法返回类型 1)ModelAndView 对象.包含Model和View对象,可以通过它访问@ModelAttribute注解的对象. 2)Model 对象.仅包含数据访问 ...

  8. 内置Web Server

    在终端输入命令:php -S localhost:8000 -t xxx(某个目录或文件) 这个内置的Web服务器主要用于本地开发使用,不可用于线上产品环境. URI请求会被发送到PHP所在的的工作目 ...

  9. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

  10. c++异常处理第四篇---不使用try catch语句,使用Loki::ScopeGuard

    转载:神奇的Loki::ScopeGuard 2011-07-05 12:52:05 分类: C/C++ 转载:http://blog.csdn.net/fangqu/article/details/ ...