本文介紹使用CodeIgniter來開發(fā)一個(gè)用戶登錄和注冊(cè)的小模塊,有詳細(xì)的數(shù)據(jù)庫表和ci代碼。
1、數(shù)據(jù)庫設(shè)計(jì)
字段 |
類型 |
空 |
額外 |
索引 |
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) 控制器
/**
* 接收、驗(yàn)證登錄表單
* 表單規(guī)則在配置文件:/config/form_validation.php
'account/login'=>array( //登錄表單的規(guī)則
array(
'field'=>'username',
'label'=>'用戶名',
'rules'=>'trim|required|xss_clean|callback_username_check'
),
array(
'field'=>'password',
'label'=>'密碼',
'rules'=>'trim|required|xss_clean|callback_password_check'
)
)
* 錯(cuò)誤提示信息在文件:/system/language/english/form_validation.php
*/
function login()
{
//設(shè)置錯(cuò)誤定界符
$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
{
//注冊(cè)session,設(shè)定登錄狀態(tài)
$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);
}
}
//登錄表單驗(yàn)證時(shí)自定義的函數(shù)
/**
* 提示用戶名是不存在的登錄
* @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數(shù)據(jù),設(shè)置用戶在線狀態(tài)
* @param string $username
*/
function login($username)
{
$data = array('username'=>$username, 'logged_in'=>TRUE);
$this->session->set_userdata($data); //添加session數(shù)據(jù)
}
/**
* 通過用戶名獲得用戶記錄
* @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;
}
}
/**
* 用戶名不存在時(shí),返回false
* 用戶名存在時(shí),驗(yàn)證密碼是否正確
*/
function password_check($username, $password)
{
if($user = $this->get_by_username($username))
{
return $user->password == $password ? TRUE : FALSE;
}
return FALSE; //當(dāng)用戶名不存在時(shí)
}
c) 視圖
4、注冊(cè)
與表單登錄的操作是相似的
a)控制器
/**
* 用戶注冊(cè)
* 表單規(guī)則在配置文件:/config/form_validation.php
'account/register'=>array( //用戶注冊(cè)表單的規(guī)則
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'=>'郵箱賬號(hào)',
'rules'=>'trim|required|xss_clean|valid_email|callback_email_exists'
)
)
* 錯(cuò)誤提示信息在文件:/system/language/english/form_validation.php
*/
function register()
{
//設(shè)置錯(cuò)誤定界符
$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);
}
}
/**
* ======================================
* 用于注冊(cè)表單驗(yàn)證的函數(shù)
* 1、username_exists()
* 2、email_exists()
* ======================================
*/
/**
* 驗(yàn)證用戶名是否被占用。
* 存在返回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;
}
/**
* 檢查郵箱賬號(hào)是否存在.
* @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、退出
/**
* 用戶退出
* 已經(jīng)登錄則退出,否者轉(zhuǎn)到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的數(shù)據(jù)
return TRUE;
}
return FALSE;
}
6、 遺留問題
a) 沒有使用驗(yàn)證碼
b) 表單規(guī)則驗(yàn)證時(shí),怎樣使當(dāng)上一個(gè)表單某項(xiàng)(如:姓名)出現(xiàn)問題時(shí),停止對(duì)后面表單項(xiàng)的驗(yàn)證(如密碼等)。比如在登錄時(shí),提示用戶名不存在,就沒必要驗(yàn)證是否填寫了密碼或者密碼有錯(cuò)誤
原文:http://www.cnblogs.com/mackxu/archive/2012/08/06/2625144.html