CodeIgniter 用戶登錄注冊模塊


  1. 數據庫設計
  2. 文件列表
  3. 登錄
  4. 注冊
  5. 退出

1、  數據庫設計

字段

類型

額外

索引

id

int(10)

auto_increment

primary key

username

varchar(20)

 

unique

password

char(32)

 

 

email

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) 控制器

View Code
 1 /**
 2      * 接收、驗證登錄表單
 3      * 表單規則在配置文件:/config/form_validation.php
 4         'account/login'=>array(                        //登錄表單的規則
 5             array(
 6                 'field'=>'username',
 7                 'label'=>'用戶名',
 8                 'rules'=>'trim|required|xss_clean|callback_username_check'
 9             ),
10             array(
11                 'field'=>'password',
12                 'label'=>'密碼',
13                 'rules'=>'trim|required|xss_clean|callback_password_check'
14             )
15         )
16      * 錯誤提示信息在文件:/system/language/english/form_validation.php
17      */
18     function login()
19     {
20         //設置錯誤定界符
21         $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
22 
23         $this->_username = $this->input->post('username');                //用戶名
24         if ($this->form_validation->run() == FALSE)
25         {
26             $this->load->view('account/login');
27         }
28         else 
29         {
30             //注冊session,設定登錄狀態
31             $this->MAccount->login($this->_username);
32             $data['message'] = $this->session->userdata('username').' You are logged in! Now take a look at the '
33                                 .anchor('account/dashboard', 'Dashboard');
34             $this->load->view('account/note', $data);
35         }
36         }
37 
38 //登錄表單驗證時自定義的函數
39 /**
40      * 提示用戶名是不存在的登錄
41      * @param string $username
42      * @return bool 
43      */
44     function username_check($username)
45     {
46         if ($this->MAccount->get_by_username($username))
47         {
48             return TRUE;
49         }
50         else 
51         {
52             $this->form_validation->set_message('username_check', '用戶名不存在');
53             return FALSE;
54         }
55     }
56     /**
57      * 檢查用戶的密碼正確性
58      */
59     function password_check($password)
60     {
61         $password = md5($this->salt.$password);
62         if ($this->MAccount->password_check($this->_username, $password))
63         {
64             return TRUE;
65         }
66         else 
67         {
68             $this->form_validation->set_message('password_check', '用戶名或密碼不正確');
69             return FALSE;
70         }
71     }

      b) 模型

View Code
 1 /**
 2      * 添加用戶session數據,設置用戶在線狀態
 3      * @param string $username
 4      */
 5     function login($username)
 6     {
 7         $data = array('username'=>$username, 'logged_in'=>TRUE);
 8         $this->session->set_userdata($data);                    //添加session數據
 9         }
10     /**
11      * 通過用戶名獲得用戶記錄
12      * @param string $username
13      */
14     function get_by_username($username)
15     {
16         $this->db->where('username', $username);
17         $query = $this->db->get('user');
18         //return $query->row();                            //不判斷獲得什么直接返回
19         if ($query->num_rows() == 1)
20         {
21             return $query->row();
22         }
23         else
24         {
25             return FALSE;
26         }
27     }
28     
29     /**
30      * 用戶名不存在時,返回false
31      * 用戶名存在時,驗證密碼是否正確
32      */
33     function password_check($username, $password)
34     {                
35         if($user = $this->get_by_username($username))
36         {
37             return $user->password == $password ? TRUE : FALSE;
38         }
39         return FALSE;                                    //當用戶名不存在時
40     }

      c) 視圖

4、  注冊

與表單登錄的操作是相似的

      a)控制器

View Code
 1 /**
 2      * 用戶注冊
 3      * 表單規則在配置文件:/config/form_validation.php
 4         'account/register'=>array(                    //用戶注冊表單的規則
 5             array(
 6                 'field'=>'username',
 7                 'label'=>'用戶名',
 8                 'rules'=>'trim|required|xss_clean|callback_username_exists'
 9             ),
10             array(
11                 'field'=>'password',
12                 'label'=>'密碼',
13                 'rules'=>'trim|required|min_length[4]|max_length[12]
14 |matches[password_conf]|xss_clean'
15             ),
16             array(
17                 'field'=>'email',
18                 'label'=>'郵箱賬號',
19                 'rules'=>'trim|required|xss_clean|valid_email|callback_email_exists'
20             )
21         )
22      * 錯誤提示信息在文件:/system/language/english/form_validation.php
23      */
24     function register()
25     {
26         //設置錯誤定界符
27         $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
28         
29         if ($this->form_validation->run() == FALSE)
30         {
31             $this->load->view('account/register');
32         }
33         else 
34         {
35             $username = $this->input->post('username');
36             $password = md5($this->salt.$this->input->post('password'));
37             $email = $this->input->post('email');
38             if ($this->MAccount->add_user($username, $password, $email))
39             {
40                 $data['message'] = "The user account has now been created! You can go "
41                             .anchor('account/index', 'here').'.';
42             }
43             else 
44             {
45                 $data['message'] = "There was a problem when adding your account. You can register "
46                             .anchor('account/register', 'here').' again.';
47             }
48             $this->load->view('account/note', $data);
49         }        
50         }
51 /**
52      * ======================================
53      * 用於注冊表單驗證的函數
54      * 1、username_exists()
55      * 2、email_exists()
56      * ======================================
57      */
58     /**
59      * 驗證用戶名是否被占用。
60      * 存在返回false, 否者返回true.
61      * @param string $username
62      * @return boolean
63      */
64     function username_exists($username)
65     {
66         if ($this->MAccount->get_by_username($username))
67         {
68             $this->form_validation->set_message('username_exists', '用戶名已被占用');
69             return FALSE;
70         }
71         return TRUE;
72     }
73     function email_exists($email)
74     {
75         if ($this->MAccount->email_exists($email))
76         {
77             $this->form_validation->set_message('email_exists', '郵箱已被占用');
78             return FALSE;
79         }
80         return TRUE;
81     }

      b)模型

View Code
 1     /**
 2      * 添加用戶
 3      */
 4     function add_user($username, $password, $email)
 5     {
 6         $data = array('username'=>$username, 'password'=>$password, 'email'=>$email);
 7         $this->db->insert('user', $data);
 8         if ($this->db->affected_rows() > 0)
 9         {
10             $this->login($username);
11             return TRUE;
12         }
13         return FALSE;
14         }
15 /**
16      * 檢查郵箱賬號是否存在.
17      * @param string $email
18      * @return boolean
19      */
20     function email_exists($email)
21     {
22         $this->db->where('email', $email);
23         $query = $this->db->get('user');
24         return $query->num_rows() ? TRUE : FALSE;
25     }

5、  退出

View Code
 1 /**
 2      * 用戶退出
 3      * 已經登錄則退出,否者轉到details
 4      */
 5     function logout()
 6     {
 7         if ($this->MAccount->logout() == TRUE)
 8         {
 9             $this->load->view('account/logout');
10         }
11         else
12         {
13             $this->load->view('account/details');
14         }
15         }
16         模型:
17     /**
18      * 注銷用戶
19      * @return boolean
20      */
21     function logout()
22     {
23         if ($this->logged_in() === TRUE)
24         {
25             $this->session->sess_destroy();                //銷毀所有session的數據
26             return TRUE;
27         }
28         return FALSE;
29     }

 6、  遺留問題

  1. 沒有使用驗證碼
  2. 表單規則驗證時,怎樣使當上一個表單某項(如:姓名)出現問題時,停止對后面表單項的驗證(如密碼等)。比如在登錄時,提示用戶名不存在,就沒必要驗證是否填寫了密碼或者密碼有錯誤


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM