预先加载数据库操作类和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. phpcms分页用法简介

    PHPCMS分页的用法 前面需要有引用的list,代码如下: {pc:content action="lists" catid="11" order=" ...

  2. STL---总结

    文章转自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/22/2603525.html 一.STL的六大组件 容器(Container),是一种 ...

  3. POJ 3322(广搜)

    ---恢复内容开始--- http://poj.org/problem?id=3322 题意:http://jandan.net/2008/01/24/bloxorz.html就是这个鬼游戏 我也是郁 ...

  4. Linux之绝处逢生------SysRq

    参考: http://www.linuxfly.org/post/545/ http://www.jb51.net/article/13525.htm 做法: 未雨绸缪 # " > / ...

  5. Starting MySQL.The server quit without updating PID file (xxxx.pid).[FAILED]

    mysql无法正常启动,查看日志报如下异常 --07T01::.929615Z [ERROR] Fatal error: Please read "Security" sectio ...

  6. [Linux]centOS7-1-1503-x86_64下安装VM-TOOLS

    1.安装Perl. 2.如果提示 The path "" is not a valid path to the 3.10.0-229.el7.x86_64 kernel heade ...

  7. UTF8编码转换(C#)

    例如: UTF8---ISO-8859-1 string string = "这是中文";Encoding utf8 = Encoding.UTF8; Encoding ISO = ...

  8. NEFU 169 步步惊心

    Description 马尔泰·若曦是康熙年间镇西大将军马尔泰的小女儿,自幼失母,却深得父亲姐姐宠爱,性格活泼任性.张晓,本是21世纪一都市白领,聪慧谨慎,玲珑剔透.因车祸而灵魂穿越到若曦身上,自此开 ...

  9. 【转】深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p

    一.可能的组合: (1)const char*p (2)char const*p (3)char *const p(4)const char **p (5)char const**p (6)char ...

  10. 分享类shareSDK

    1.新浪微博分享时需要注意: [A] 应用信息->基本信息->应用地址 [B] 应用信息->高级信息->OAuth2.0 授权设置 //当使用新浪微博客户端分享的时候需要按照下 ...