CI登录验证
预先加载数据库操作类和Session类 即在autoload.php中,$autoload['libraries'] = array('database', 'session');
a. 注: 使用session , 要设定 encryption key : config.php中: $config['encryption_key'] = '!@#$%^&*()';
登录表单页(view) : login_view.php
注: 由于该页面使用了CI的form标签, 所以需要在渲染该页面前加载form_helper, 即: 在config/autoload.php, 文件中加入: $autoload['helper'] = array('url', 'form');
html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="utf-8">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Login</title>
<style>div{display: block;} .errors{color: red;}</style>
</head>
<body>
<h1>Please Login</h1>
<?php echo form_open('admin');?>
<p>
<?php
echo form_label('Email: ', 'email');
echo form_input('email', set_value('email'), 'id="email" autofocus'); // set_value 如果密码输入有误, 返回时,默认写入值
?>
</p>
<p>
<?php
echo form_label('Password: ', 'password');
echo form_input('password', '', 'id="password"');
?>
</p>
<p>
<?php echo form_submit('submit','Login');?>
</p>
<?php echo form_close();?> <div class="errors"><?php echo validation_errors();?></div>
</body> </html>
admin.php控制器 php代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
// 在welcome的action中添加如下代码,即可用户登录情况
/**
* public function __costruct(){
* session_start();
* parent::_construct();
*
* if(!$this->session->userdata('username')) redirect('admin');
* }
*/
if ($this->session->userdata('username')) {
redirect('welcome');
}
$this->load->library('form_validation'); // 使用CI的表单验证, 如下:
$this->form_validation->set_rules('email', 'Email', 'valid_email|required');
$this->form_validation->set_rules('password', 'Password', 'min_length[4]|required');
if($this->form_validation->run() !== false){
// then validate password. Get from the Db.
$this->load->model('admin_model');
$res = $this->admin_model->verify_users(
$this->input->post('email'),
$this->input->post('password')
);
if($res !== false){
print_r($res);
$this->session->set_userdata('username', $this->input->post('email'));
redirect('welcome');
}
}
$this->load->view('login_view');
}
public function logout(){
$this->session->sess_destroy();
$this->load->view('login_view');
}
}
admin_model.php模型 php代码: <?php
class Admin_model extends CI_Model{
function verify_users($email, $password){
$q = $this->db
->where('email', $email)
->where('password', sha1($password))
->limit(1)->get('users');
if($q->num_rows > 0){
return $q->row();
}
return false;
}
}

CI登录验证的更多相关文章
- ASP.NET MVC 登录验证
好久没写随笔了,这段时间没 什么事情,领导 一直没安排任务,索性 一直在研究代码,说实在的,这个登录都 搞得我云里雾里的,所以这次我可能也讲得不是 特别清楚,但是 我尽力把我知道的讲出来,顺便也对自 ...
- Shiro安全框架入门篇(登录验证实例详解与源码)
转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...
- 练习:python 操作Mysql 实现登录验证 用户权限管理
python 操作Mysql 实现登录验证 用户权限管理
- AD域登录验证
AD域登录验证 作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5799699.html 需求 系统在登录的时候,需要根据用户名和密码验证连接域服务器进行 ...
- ASP.NET MVC4 Forms 登录验证
Web.config配置: 在<system.web>节下: <authentication mode="Forms"> <forms loginUr ...
- MVC前台页面做登录验证
最近接触了一个电商平台的前台页面,需要做一个登录验证,具体情况是:当用户想要看自己的订单.积分等等信息,就需要用户登录之后才能查询,那么在MVC项目中我们应该怎么做这个前台的验证呢? 1.我在Cont ...
- [MVC学习笔记]5.使用Controller来代替Filter完成登录验证(Session校验)
之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承ActionFilterAttribute类,重写On ...
- ThinkPHP之登录验证
验证方面写的不是很完整,正在完善当中 <?php /** * Created by dreamcms. * User: Administrator * Date: 2016/9/5 * Time ...
- ASP.NET MVC3 实现用户登录验证
自定义一个授权筛选器类,继承于AuthorizeAttribute: using System; using System.Web; using System.Web.Mvc; namespace M ...
随机推荐
- python wraps
用代码说明问题: def d(f): def _d(*args, **kwargs): print f.__name__, ' is called' f(*args, **kwargs) return ...
- ndk学习15: IPC机制
Linux IPC机制 来自为知笔记(Wiz)
- python查找并删除相同文件-UNIQ File-wxPython版本
今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...
- oracle 存储过程中调用存储过程
create procedure sp_name() begin ……… end 比如: create procedure pro_showdbs() show datebase; end //用ex ...
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- ModelState.IsValid
model内的设置如下所示: /// <summary> /// 取得或设置邮编 /// </summary> [RegularExpression(@"(^[1-9 ...
- React JS快速入门教程
翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- ACM/ICPC 之 枚举(POJ1681-画家问题+POJ1166-拨钟问题+POJ1054-讨厌的青蛙)
POJ1681-画家问题 枚举的经典例题,枚举第一行即可,其余行唯一. //画家问题,y表示黄色,w表示白色,怎样让墙上所有方格为y,操作类似熄灯问题poj1222 //memory 136K Tim ...
- MongoDB 分片管理(不定时更新)
背景: 通过上一篇的 MongoDB 分片的原理.搭建.应用 大致了解了MongoDB分片的安装和一些基本的使用情况,现在来说明下如何管理和优化MongoDB分片的使用. 知识点: 1) 分片的配置和 ...