预先加载数据库操作类和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登录验证的更多相关文章

  1. ASP.NET MVC 登录验证

     好久没写随笔了,这段时间没 什么事情,领导 一直没安排任务,索性 一直在研究代码,说实在的,这个登录都 搞得我云里雾里的,所以这次我可能也讲得不是 特别清楚,但是 我尽力把我知道的讲出来,顺便也对自 ...

  2. Shiro安全框架入门篇(登录验证实例详解与源码)

    转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...

  3. 练习:python 操作Mysql 实现登录验证 用户权限管理

    python 操作Mysql 实现登录验证 用户权限管理

  4. AD域登录验证

    AD域登录验证 作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5799699.html 需求 系统在登录的时候,需要根据用户名和密码验证连接域服务器进行 ...

  5. ASP.NET MVC4 Forms 登录验证

    Web.config配置: 在<system.web>节下: <authentication mode="Forms"> <forms loginUr ...

  6. MVC前台页面做登录验证

    最近接触了一个电商平台的前台页面,需要做一个登录验证,具体情况是:当用户想要看自己的订单.积分等等信息,就需要用户登录之后才能查询,那么在MVC项目中我们应该怎么做这个前台的验证呢? 1.我在Cont ...

  7. [MVC学习笔记]5.使用Controller来代替Filter完成登录验证(Session校验)

          之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承ActionFilterAttribute类,重写On ...

  8. ThinkPHP之登录验证

    验证方面写的不是很完整,正在完善当中 <?php /** * Created by dreamcms. * User: Administrator * Date: 2016/9/5 * Time ...

  9. ASP.NET MVC3 实现用户登录验证

    自定义一个授权筛选器类,继承于AuthorizeAttribute: using System; using System.Web; using System.Web.Mvc; namespace M ...

随机推荐

  1. NOIP 2010题解

    唔..NOIP2010比较简单,总体感觉不错. Problem 1: 机器翻译 水题,队列的简单应用. 读入时判断是否在内存中,可以用hash优化.如果不在内存中push进内存,放不下了pop hea ...

  2. ASP.NET MVC Area使用-将Area设置成独立项目

    环境说明:Vistual Studio 2013 MVC 4.0 其实关于ASP.NET MVC Area使用的基础知识可以参考 http://www.cnblogs.com/willick/p/33 ...

  3. devstack meaning of: n-cond, n-novnc and n-xvnc

    devstack has shortened names for a number of services, e.g. g-api = glance api g-reg = glance regist ...

  4. 在中文windows下使用pywinauto进行窗口操作

    这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官 ...

  5. poj1155

    题意:给定一个树形图,节点数量3000.叶子节点是用户,每个用户如果能看上电视会交一定的电视费.看上电视的条件是从根到该用户的路径全部被修好,修每条边有一个费用.在不亏损(用户交钱总额>=修路总 ...

  6. Qt 对话框显示控制按钮

    在对话框窗体构造函数加入 SystemDialog::SystemDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SystemDialog) ...

  7. perl运行其他程序的5种方法

    1.使用system函数 运行成功,返回0,运行失败则返回非负整数 system("cmd"); 2.使用qx my $cmd1=qx/date/; 3.使用`` 与qx等效 4. ...

  8. 解决 vs2010 联接sql 2005 时 报错未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc

    http://blogs.msdn.com/b/sqlnativeclient/archive/2008/05/30/sqlncli-msi-for-sql-server-2008.aspx 关键是这 ...

  9. Java for LeetCode 208 Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  10. js 中 toString( ) 和valueOf( )

    1.toString()方法:主要用于Array.Boolean.Date.Error.Function.Number等对象转化为字符串形式.日期类的toString()方法返回一个可读的日期和字符串 ...