zoukankan      html  css  js  c++  java
  • 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. 表单规则验证时,怎样使当上一个表单某项(如:姓名)出现问题时,停止对后面表单项的验证(如密码等)。比如在登录时,提示用户名不存在,就没必要验证是否填写了密码或者密码有错误
  • 相关阅读:
    【CodeForces 611C】New Year and Domino
    【HDU 1003】 Max Sum
    【ZOJ 3502】Contest
    【LightOJ 1422】Halloween Costumes(区间DP)
    【ZOJ 3609】Modular Inverse
    乘法逆元及其应用
    除法和取余的运算时间
    HUD3689 Infinite monkey theorem
    POJ2185 Milking Grid
    1355: [Baltic2009]Radio Transmission[循环节]
  • 原文地址:https://www.cnblogs.com/mackxu/p/2625144.html
Copyright © 2011-2022 走看看