本文介紹使用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