user 的 controller 和 view

Controller: user.php

<?php
class User extends CI_Controller {
    public function __construct()
    {
            parent::__construct();
    }
    public function index()
    {
        // if posted with a username, validate login or generate error message
        if (isset($_POST['username'])) {
        
            $this->load->model('MUser', '', TRUE);
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            if ($this->MUser->authenticate_user($username, $password, $uid, $is_admin) ) {
                $this->session->set_userdata('uid', $uid);
                $this->session->set_userdata('is_admin', $is_admin);
                redirect('', 'refresh');
            } else {
                $data['error_message'] = '<p>Incorrect Username and / or Password</p>';
            }
        }
        // if not posted (or a authentication error), simply load the form
        
        $this->load->helper('form');
        // display information for the view
        $data['title'] = "Classroom: User Login";
        $data['headline'] = "User Login";
        $data['include'] = 'user_login';
        $data['uid'] = $this->session->userdata('uid');
        $data['is_admin'] = $this->session->userdata('is_admin');
        $this->load->view('template', $data);
    }
    public function register()
    {
        // if posted with a username, validate login or generate error message
        if (isset($_POST['username'])) {
            $this->load->model('MUser', '', TRUE);
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            $this->load->library('form_validation');
                
            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]');
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
                
            $has_error = FALSE;
            $data['error_message'] = NULL;
            if ($this->MUser->username_exists($username)) {
                $has_error = TRUE;
                $data['error_message'] .= '<p>That Username is already taken. Please choose another.</p>';
            }
            if ($this->form_validation->run() == FALSE) {
                $has_error = TRUE;
                $data['error_message'] .= validation_errors();
            }
            if ( ! $has_error) {
                $this->MUser->add_user($username, $password, $uid, $is_admin);
                $this->session->set_userdata('uid', $uid);
                $this->session->set_userdata('is_admin', $is_admin);
                $data['success_message'] = '<p>Congratulations! Your registration was successful.</p>';
                $data['register_success'] = TRUE;
                $data['username'] = $username;
                $data['password'] = $password;
            }
        }
        // if not posted (or a authentication error), simply load the form
        
        $this->load->helper('form');
        // display information for the view
        $data['title'] = "Classroom: User Registration";
        $data['headline'] = "User Registration";
        $data['include'] = 'user_register';
        $data['uid'] = $this->session->userdata('uid');
        $data['is_admin'] = $this->session->userdata('is_admin');
        $this->load->view('template', $data);
    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('', 'refresh');
    }
}
/* End of file user.php */
/* Location: ./application/controllers/user.php */

View user_login.php

<?php
    echo form_open('user', array('id' => 'login_form'));
    
    // an array of the fields in the student table
    $field_array = array('username' => 'Username','password' => 'Password');
    foreach($field_array as $field => $label) {
        echo '<p>';
        echo form_label($label, $field);
        if (substr($label,0,8) == 'Password') {
            echo form_password(array('name' => $field, 'id' => $field ));
        } else {
            echo form_input(array('name' => $field, 'id' => $field, 'value' => set_value($field) ));
        }
        echo '</p>';
    }
    
    // not setting the value attribute omits the submit from the $_POST array
    echo form_submit('', 'Login');
    
    echo form_close();
/* End of file user_login.php */
/* Location: ./application/views/user_login.php */

View user_register.php

<?php
    if (isset($register_success)) {
        echo '<p><label for="username">Username:</label>'.$username.'</p>';;
        echo '<p><label for="password">Password:</label>'.$password.'</p>';;
    } else {
        echo form_open('user/register', array('id' => 'registration_form'));
        
        // an array of the fields in the student table
        $field_array = array('username' => 'Username','password' => 'Password','passconf' => 'Password Confirmation');
        foreach($field_array as $field => $label) {
            echo '<p>';
            echo form_label($label, $field);
            if (substr($label,0,8) == 'Password') {
                echo form_password(array('name' => $field, 'id' => $field ));
            } else {
                echo form_input(array('name' => $field, 'id' => $field, 'value' => set_value($field) ));
            }
            echo '</p>';
        }
        // not setting the value attribute omits the submit from the $_POST array
        echo form_submit('', 'Register');
        
        echo form_close();
    }
    
/* End of file user_register.php */
/* Location: ./application/views/user_register.php */