06-后台登录,验证用户名和密码
https://v.youku.com/v_show/id_XMTM2NDYzMzAyMA==.html
...
前台js验证
<script type="text/javascript">
$(function() {
/* 验证输入不能为空*/
$('#login_form form').submit(function(){
if ($('input[name="username"]').val() == '') {
$('input[name="username"]').focus();
return false;
}
if ($('input[name="password"]').val() == '') {
$('input[name="password"]').focus();
return false;
}
if ($('input[name="verify"]').val() == '') {
$('input[name="verify"]').focus();
return false;
}
});
$('img#verify').click(function(){ /* img#verify 是下面图片的标签名和ID*/
$.ajax({
url: '<?php echo site_url(MODULE .'/'. C .'/captcha') ?>?i='+Math.random(),
success: function(data){
/*alert(data); 调试,查看图片地址*/
$('img#verify').attr('src',data);
return;
}
});
})
})
</script>
...
数据库建表和数据
create table user (id tinyint(1),username varchar(20),password char(32),status tinyint(1));
insert into user (id,username,password,status) values (1,'admin','123','1');
...
新建user_model模型
1,模型类在models目录下创建。
2,模型类必须继承CI_Model。
新建User_model.php
<?
defined('BASEPATH') || exit('No direct script access allowed');
class User_model extends CI_Model {
// 把用户名密码传递到这里
private $t ='user'; //声明一个私有变量,表名
public function login($where = array()){
if (empty($where)) {
return array();
}
//return $this->db->get_where('ci_user'),$where)->row_array();
return $this->db->get_where($this-t,$where)->row_array();
}
}
控制器login.php方法
public function index() {
$data = array();
if (! empty($_POST)) {
$username = $this->input->post('username',NULL);
$password = $this->input->post('password',NULL);
$verify = $this->input->post('verify',NULL);
if (empty($username)||empty($password)||empty($password)) {
echo "<script>alert('用户名,密码,验证码都不能为空');history.back();</script>";
exit();
}
if ($verify != $_SESSION['verify_code']) {
echo "<script>alert('验证码不正确!');history.back();</script>";
exit();
}
//验证码正确后,就要释放Session中的验证码,清空sessiion中的验证码。
unset($_SESSION['verify_code']);
$_SESSION['verify_code'] = NULL;
//p($_POST);
//加载模块
$this->load->model(MODULE.'/User_model');
$where = array(
'username' => $username,
'password' => $this->_password($password)
);
// 用变量接收返回值
$user = $this->User_model->login($where);
if (empty($user)) { // 如果返回空记录
echo "<script>alert('用户名或密码错误!');history.back();</script>";
exit();
}
if (empty($user['status'])){
echo "<script>alert('你已经被禁用,请联系管理员!');history.back();</script>";
exit();
}
redirect(MODULE .'/main/index');
exit();
}
$data['captcha'] = $this->_captcha();
//exit($captcha);
$this->load->view(MODULE .'/user/login',$data); // $this->load->view('admin/user/login');
//var_dump($data);
}
My_Controller.php
<?php
defined('BASEPATH') || exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct(){
parent :: __construct();
session_start();
// 定义常量
define('PUB','public');
define('MODULE','admin');
define('C',$this->uri->segment(2,'main')); // class
define('M',$this->uri->segment(3,'index')); // mothed
}
// 密码加密
protected function _password($pass) {
return md5(md5($pass) . 'ci'); // 两次加密并加随意字符串
}
protected function _captcha() {
$this->load->helper('captcha');
$_SESSION['verify_code'] = rand(10000,99999);
$config = array(
'word' => $_SESSION['verify_code'], //'Random word',
'img_path' => './'.PUB . '/captcha/', //图片保存的路径
'img_url' => base_url('public/captcha'), // 图片地址
'font_path' => './system/fonts/texb.ttf', // 字体路径
'img_width' => 150,
'img_height' => 30,
'expiration' => 7200 //过期时间7200秒
);
$captcha = create_captcha($config);
//print_r($captcha);
return $config['img_url'].'/'.$captcha['filename'];
}
}