|
发表于 2011-7-16 14:48:19
|
显示全部楼层
本帖最后由 √__豿艉騲℡ 于 2011-7-16 14:55 编辑
昨天刚刚也在弄登陆注册。
本人新手菜鸟,欢迎高手指导……
Controller
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
/**
* CLASS USER 构造函数
*
* @access public
* @return void
*/
function __construct () {
parent ::__construct ();
header('Content-Type: text/html; charset=utf-8');
}
/**
* 登陆
*
* @access public
* @return void
*/
function login () {
header('refresh:5; url=' . site_url ());
$this->form_validation->set_rules('username', '用户名', 'trim|required|min_length[5]|max_length[16]');
$this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[16]');
if ($this->User_model->login() > 0) {
echo '登陆成功';
} else {
echo '登陆失败';
}
echo anchor ('', '立即返回');
}
/**
* 加载注册视图
*
* @access public
* @return void
*/
function registration () {
$this->load->view('registration_view');
}
/**
* 注册
*
* @access public
* @return void
*/
function regist () {
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('username', '用户名', 'trim|required|min_length[5]|max_length[16]|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[16]');
$this->form_validation->set_rules('passconf', '确认密码', 'trim|required|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_view');
} else {
header('refresh:5; url=' . site_url ());
if ($this->User_model->regist()) {
echo '注册成功.';
} else {
echo '注册失败.';
}
echo anchor ('', '立即返回.');
}
}
/**
* 检查用户名是否存在
*
* @access public
* @param mixed $str
* @return bool
*/
function username_check ($str) {
if ($this->User_model->isusername($str) > 0) {
$this->form_validation->set_message('username_check', $str . ' 已注册.');
$bool = false;
} else {
$bool = true;
}
return $bool;
}
/**
* 会员登出
*
* @access public
* @return void
*/
function logout () {
$this->session->sess_destroy();
header('refresh:5; url=' . site_url ());
echo '已退出.' . anchor ('', '立即返回.');
}
}
复制代码
model
PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
/**
* 会员登陆函数
*
* @access public
* @param string $table
* @return Int
*/
function login ($table = 'user') {
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get($table);
if ($query->num_rows() > 0) {
$row = $query->row();
$newdata = array(
'username' => $row->username,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
}
return $query->num_rows();
}
/**
* 会员注册函数
*
* @access public
* @param string $table
* @return TRUE/FALSE
*/
function regist ($table = 'user') {
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
return $this->db->insert($table, $data);
}
/**
* 判断用户名是否存在
*
* @access public
* @param mixed $username
* @param string $table
* @return void
*/
function isusername ($username, $table = 'user') {
$this->db->where('username', $username);
$query = $this->db->get($table);
return $query->num_rows();
}
}
复制代码 |
|