ci用户登录

[list]
预先加载数据库操作类和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代码   收藏代码
  1.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.     <html lang="utf-8">  
  3.         <head>  
  4.             <meta http-equiv="Content-type" content="text/html; charset=utf-8" />  
  5.             <title>Login</title>  
  6.             <style>div{display: block;} .errors{color: red;}</style>  
  7.         </head>  
  8.         <body>  
  9.             <h1>Please Login</h1>  
  10.             <?php echo form_open('admin');?>  
  11.             <p>  
  12.                 <?php   
  13.                     echo form_label('Email: ', 'email');  
  14.                     echo form_input('email', set_value('email'), 'id="email" autofocus'); // set_value 如果密码输入有误, 返回时,默认写入值  
  15.                 ?>  
  16.             </p>  
  17.             <p>  
  18.                 <?php   
  19.                     echo form_label('Password: ', 'password');  
  20.                     echo form_input('password', '', 'id="password"');  
  21.                 ?>  
  22.             </p>  
  23.             <p>  
  24.                 <?php echo form_submit('submit','Login');?>  
  25.             </p>  
  26.             <?php echo form_close();?>  
  27.               
  28.             <div class="errors"><?php echo validation_errors();?></div>  
  29.         </body>  
  30.   
  31. </html>  
控制器 : admin.php
Php代码   收藏代码
  1.     <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  2.       
  3.     class Admin extends CI_Controller {  
  4.           
  5.         function __construct(){  
  6.             parent::__construct();  
  7.         }  
  8.           
  9.         public function index(){  
  10.               
  11.             // 在welcome的action中添加如下代码,即可用户登录情况  
  12.             /** 
  13.              * public function __costruct(){ 
  14.              *      session_start(); 
  15.              *      parent::_construct(); 
  16.              *  
  17.              *      if(!$this->session->userdata('username')) redirect('admin'); 
  18.              * } 
  19.              */  
  20.             if ($this->session->userdata('username')) {  
  21.                 redirect('welcome');  
  22.             }  
  23.               
  24.             $this->load->library('form_validation'); // 使用CI的表单验证, 如下:  
  25.             $this->form_validation->set_rules('email', 'Email', 'valid_email|required');  
  26.             $this->form_validation->set_rules('password', 'Password', 'min_length[4]|required');  
  27.               
  28.             if($this->form_validation->run() !== false){  
  29.                 // then validate password. Get from the Db.  
  30.                 $this->load->model('admin_model');  
  31.                 $res = $this->admin_model->verify_users(  
  32.                                                 $this->input->post('email'),  
  33.                                                 $this->input->post('password')  
  34.                                             );  
  35.                 if($res !== false){  
  36.                                             print_r($res);  
  37.                     $this->session->set_userdata('username', $this->input->post('email'));  
  38.                     redirect('welcome');   
  39.                 }  
  40.             }  
  41.               
  42.             $this->load->view('login_view');  
  43.         }  
  44.           
  45.         public function logout(){  
  46.             $this->session->sess_destroy();  
  47.             $this->load->view('login_view');  
  48.         }  
  49.   
  50. }  
模型层admin_model.php 注: 由于使用了数据库, 在这里必须首先加载数据库连接: 即: 在config/autoload.php, 文件中加入: $autoload['libraries'] = array('database');
Php代码   收藏代码
  1. <?php  
  2.   
  3. class Admin_model extends CI_Model{  
  4.   
  5.     function verify_users($email, $password){  
  6.         $q = $this->db  
  7.             ->where('email', $email)  
  8.             ->where('password', sha1($password))  
  9.             ->limit(1)->get('users');  
  10.               
  11.         if($q->num_rows > 0){  
  12.             return $q->row();  
  13.         }  
  14.         return false;  
  15.     }  
  16. }  

[/list]

转载于:https://www.cnblogs.com/zoupufa/p/4262790.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值