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 ...
随机推荐
- [mysql] Some non-transactional changed tables couldn't be rolled back
使用peewee的事务时,碰到一个郁闷的问题,事务似乎无效! 于是简化了下模型,写了简单的测试代码,发现问题,如题所示. 找到解答: https://github.com/etianen/django ...
- svn: E155004 'XX' is already locked
Error:svn: E155004: Run 'svn cleanup' to remove locks (type 'svn help cleanup' for details)svn: E155 ...
- Docker 存储设置
docker默认单个容器可以使用数据空间大小10GB,docker可用数据总空间100GB,元数据可用总空间2GB.用docker info信息可以查看Data Space Total.Metadat ...
- C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)
数组是变量的索引列表,可以在方括号中指定索引来访问数组中的各个成员,其中索引是一个整数,从0开始. 一维数组 多维数组(矩形数组) 数组的数组(锯齿数组) 数组必须在访问之前初始化,数组的初始化有两种 ...
- Python之队列queue模块使用 常见问题与用法
python 中,队列是线程间最常用的交换数据的形式.queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外. 1. 阻塞模式 import queue q = queu ...
- Unity3d UGUI序列帧动画
代码 using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine. ...
- 压测 apache ab 初探
2015年10月30日 14:58:34 ab是apache自带的压测命令, 在其bin目录下边, 不仅可以压测Apache, 也可以测nginx或其他服务器 可以模拟上传post值 (-p, 与下边 ...
- How to call getClass() from a static method in Java?
刚才在学习Java 使用properties类,遇到这样的错误: Cannot make a static reference to the non-static method getClass() ...
- 25. javacript高级程序设计-新兴的API
1. 新兴的API requestAnimationFrame():是一个着眼于优化js动画的api,能够在动画运行期间发出信号.通过这种机制,浏览器就能够自动优化屏幕重绘操作 Page Visibi ...
- STL_fill()用法
以前很多次会给某个区间赋值,今天才知道有个函数可以满足这个功能. void fill (ForwardIterator first, ForwardIterator last, const T& ...