[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');
	<!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 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
注: 由于使用了数据库, 在这里必须首先加载数据库连接: 即: 在config/autoload.php, 文件中加入: $autoload['libraries'] = array('database');
	<?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;
		}
	}


[/list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值