本文介绍使用CodeIgniter来开发一个用户登录和注册的小模块,有详细的数据库表和ci代码。
1、数据库设计
字段 |
类型 |
空 |
额外 |
索引 |
id |
int(10) |
否 |
auto_increment |
primary key |
username |
varchar(20) |
否 |
|
unique |
password |
char(32) |
否 |
|
|
|
varchar(50) |
否 |
|
unique |
2、文件列表
控制器:Account.php
模型:Maccount.php
视图:
account/dashboard.php
account/details.php
account/login.php
account/logout.php
account/note.php
account/register.php
3、登录
a) 控制器
/**
* 接收、验证登录表单
* 表单规则在配置文件:/config/form_validation.php
'account/login'=>array( //登录表单的规则
array(
'field'=>'username',
'label'=>'用户名',
'rules'=>'trim|required|xss_clean|callback_username_check'
),
array(
'field'=>'password',
'label'=>'密码',
'rules'=>'trim|required|xss_clean|callback_password_check'
)
)
* 错误提示信息在文件:/system/language/english/form_validation.php
*/
function login()
{
//设置错误定界符
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
$this->_username = $this->input->post('username'); //用户名
if ($this->form_validation->run() == FALSE)
{
$this->load->view('account/login');
}
else
{
//注册session,设定登录状态
$this->MAccount->login($this->_username);
$data['message'] = $this->session->userdata('username').' You are logged in! Now take a look at the '
.anchor('account/dashboard', 'Dashboard');
$this->load->view('account/note', $data);
}
}
//登录表单验证时自定义的函数
/**
* 提示用户名是不存在的登录
* @param string $username
* @return bool
*/
function username_check($username)
{
if ($this->MAccount->get_by_username($username))
{
return TRUE;
}
else
{
$this->form_validation->set_message('username_check', '用户名不存在');
return FALSE;
}
}
/**
* 检查用户的密码正确性
*/
function password_check($password)
{
$password = md5($this->salt.$password);
if ($this->MAccount->password_check($this->_username, $password))
{
return TRUE;
}
else
{
$this->form_validation->set_message('password_check', '用户名或密码不正确');
return FALSE;
}
}
b) 模型
/**
* 添加用户session数据,设置用户在线状态
* @param string $username
*/
function login($username)
{
$data = array('username'=>$username, 'logged_in'=>TRUE);
$this->session->set_userdata($data); //添加session数据
}
/**
* 通过用户名获得用户记录
* @param string $username
*/
function get_by_username($username)
{
$this->db->where('username', $username);
$query = $this->db->get('user');
//return $query->row(); //不判断获得什么直接返回
if ($query->num_rows() == 1)
{
return $query->row();
}
else
{
return FALSE;
}
}
/**
* 用户名不存在时,返回false
* 用户名存在时,验证密码是否正确
*/
function password_check($username, $password)
{
if($user = $this->get_by_username($username))
{
return $user->password == $password ? TRUE : FALSE;
}
return FALSE; //当用户名不存在时
}
c) 视图
4、注册
与表单登录的操作是相似的
a)控制器
/**
* 用户注册
* 表单规则在配置文件:/config/form_validation.php
'account/register'=>array( //用户注册表单的规则
array(
'field'=>'username',
'label'=>'用户名',
'rules'=>'trim|required|xss_clean|callback_username_exists'
),
array(
'field'=>'password',
'label'=>'密码',
'rules'=>'trim|required|min_length[4]|max_length[12]
|matches[password_conf]|xss_clean'
),
array(
'field'=>'email',
'label'=>'邮箱账号',
'rules'=>'trim|required|xss_clean|valid_email|callback_email_exists'
)
)
* 错误提示信息在文件:/system/language/english/form_validation.php
*/
function register()
{
//设置错误定界符
$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('account/register');
}
else
{
$username = $this->input->post('username');
$password = md5($this->salt.$this->input->post('password'));
$email = $this->input->post('email');
if ($this->MAccount->add_user($username, $password, $email))
{
$data['message'] = "The user account has now been created! You can go "
.anchor('account/index', 'here').'.';
}
else
{
$data['message'] = "There was a problem when adding your account. You can register "
.anchor('account/register', 'here').' again.';
}
$this->load->view('account/note', $data);
}
}
/**
* ======================================
* 用于注册表单验证的函数
* 1、username_exists()
* 2、email_exists()
* ======================================
*/
/**
* 验证用户名是否被占用。
* 存在返回false, 否者返回true.
* @param string $username
* @return boolean
*/
function username_exists($username)
{
if ($this->MAccount->get_by_username($username))
{
$this->form_validation->set_message('username_exists', '用户名已被占用');
return FALSE;
}
return TRUE;
}
function email_exists($email)
{
if ($this->MAccount->email_exists($email))
{
$this->form_validation->set_message('email_exists', '邮箱已被占用');
return FALSE;
}
return TRUE;
}
b)模型
/**
* 添加用户
*/
function add_user($username, $password, $email)
{
$data = array('username'=>$username, 'password'=>$password, 'email'=>$email);
$this->db->insert('user', $data);
if ($this->db->affected_rows() > 0)
{
$this->login($username);
return TRUE;
}
return FALSE;
}
/**
* 检查邮箱账号是否存在.
* @param string $email
* @return boolean
*/
function email_exists($email)
{
$this->db->where('email', $email);
$query = $this->db->get('user');
return $query->num_rows() ? TRUE : FALSE;
}
5、退出
/**
* 用户退出
* 已经登录则退出,否者转到details
*/
function logout()
{
if ($this->MAccount->logout() == TRUE)
{
$this->load->view('account/logout');
}
else
{
$this->load->view('account/details');
}
}
模型:
/**
* 注销用户
* @return boolean
*/
function logout()
{
if ($this->logged_in() === TRUE)
{
$this->session->sess_destroy(); //销毁所有session的数据
return TRUE;
}
return FALSE;
}
6、 遗留问题
a) 没有使用验证码
b) 表单规则验证时,怎样使当上一个表单某项(如:姓名)出现问题时,停止对后面表单项的验证(如密码等)。比如在登录时,提示用户名不存在,就没必要验证是否填写了密码或者密码有错误
原文:http://www.cnblogs.com/mackxu/archive/2012/08/06/2625144.html