ci制作登陆+验证码
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 登陆控制器
*/
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function login(){
$msg='';
if (IS_POST) {
// 接收登陆信息
$username=$this->input->post('username');
$pwd=$this->input->post('pwd');
// 验证验证码
$captcha=$this->input->post('captcha');
$cap_word=$this->session->userdata('cap_word');
if (strtolower($captcha)!=$cap_word) {
echo "验证码错误";die;
}
// 验证登陆信息
$post=array('username'=>$username,'pwd'=>$pwd);
$user=$this->db->get_where('ci_login',$post)->row_array();
if ($user) {
$this->session->set_userdata('user_info', true);
$this->session->set_userdata('user', $user);
redirect('Index/index');
}else{
echo "用户名或密码错误";die;
}
}else{
$captcha=$this->captcha();
$this->load->vars('cap_image',$captcha);
$this->load->vars('msg',$msg);
$this->load->view('admin/login.html');
}
}
// 退出登陆
public function delete(){
$this->session->sess_destroy();
redirect('admin/Login/login');
}
/**
* 生成验证码
*/
public function captcha(){
// 加载验证码类
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',//需要在根目录上创建此文件夹
'img_url' => base_url().'captcha',//保存文件地址
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 5,
'word_length' => 4,
'font_size' => 16,
'pool' => '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ',
);
$cap = create_captcha($vals);
$this->session->set_userdata('cap_word',strtolower($cap['word']));
// var_dump($cap);
return $cap['image'];
}
/**
* js刷新验证码
*/
public function create_captcha(){
$cap=$this->captcha();
// var_dump($cap);
echo $cap;
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 登陆控制器
*/
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function login(){
$msg='';
if (IS_POST) {
// 接收登陆信息
$username=$this->input->post('username');
$pwd=$this->input->post('pwd');
// 验证验证码
$captcha=$this->input->post('captcha');
$cap_word=$this->session->userdata('cap_word');
if (strtolower($captcha)!=$cap_word) {
echo "验证码错误";die;
}
// 验证登陆信息
$post=array('username'=>$username,'pwd'=>$pwd);
$user=$this->db->get_where('ci_login',$post)->row_array();
if ($user) {
$this->session->set_userdata('user_info', true);
$this->session->set_userdata('user', $user);
redirect('Index/index');
}else{
echo "用户名或密码错误";die;
}
}else{
$captcha=$this->captcha();
$this->load->vars('cap_image',$captcha);
$this->load->vars('msg',$msg);
$this->load->view('admin/login.html');
}
}
// 退出登陆
public function delete(){
$this->session->sess_destroy();
redirect('admin/Login/login');
}
/**
* 生成验证码
*/
public function captcha(){
// 加载验证码类
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',//需要在根目录上创建此文件夹
'img_url' => base_url().'captcha',//保存文件地址
'font_path' => './path/to/fonts/texb.ttf',
'img_width' => '150',
'img_height' => 30,
'expiration' => 5,
'word_length' => 4,
'font_size' => 16,
'pool' => '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ',
);
$cap = create_captcha($vals);
$this->session->set_userdata('cap_word',strtolower($cap['word']));
// var_dump($cap);
return $cap['image'];
}
/**
* js刷新验证码
*/
public function create_captcha(){
$cap=$this->captcha();
// var_dump($cap);
echo $cap;
}
}