login multiuser codeigniter 2.0.2

config/autoload

$autoload['libraries'] = array('database','session','form_validation');
$autoload['helper'] = array('url');

config/config

$config['base_url']	= 'http://localhost/ci_cobo/';

config/database

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'inemos';
$db['default']['dbdriver'] = 'mysql';

config/route

$route['default_controller'] = "pages/login";

controller/pages

class pages extends CI_Controller{
    //put your code here
    var $template = 'template';
    
    public function __construct() {
        parent::__construct();
    }
    /*
     * home view
     */
    public function login(){
        $data['content'] = 'user/login';
        $this->load->view($this->template,$data);
    }
    
}

Controller/users

class users extends CI_Controller{
    //put your code here
    var $template = 'template';
    
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }
    /*
     * getting variable $username and $password from view/login.php
     */
    public function login(){
        $this->form_validation->set_rules('email', 'email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'password', 'required');
        $this->form_validation->set_error_delimiters('', '<br/>');
        
         if ($this->form_validation->run() == TRUE) {
            $email = $this->input->post('email');
            $password = $this->input->post('password');
            
            //call model for user login
            $user = $this->user_model->cekLogin($email, $password);
            
            if (!empty($user)) {
                $sessionData['id'] = $user['id'];
                $sessionData['email'] = $user['email'];
                $sessionData['full_name'] = $user['full_name'];
                $sessionData['level'] = $user['level'];
                $sessionData['is_login'] = TRUE;
                /**
                 * setting session in session data
                 */
                $this->session->set_userdata($sessionData);
                
                if ($this->session->userdata('level') == 1) {
                    redirect('admin/dashboard');
                } else {
                    redirect('users/home');
                }
            }
            
            $this->session->set_flashdata('message', 'Login Gagal!, email dan password tidak sesuai');
            redirect('users/login'); 
         }
        $data['content'] = 'user/login';
        $this->load->view($this->template, $data);
    }
    /**
     * after login selecting by user send to users/home
     */
    function home() {
        $this->users_library->cekUserLogin();
        $data['content'] = 'user/home';
        $this->load->view($this->template, $data);
    }
    function logout() {

        $this->session->sess_destroy();
        redirect('users/login');
    }
}

Controller/admin/dashboard

class dashboard extends CI_Controller{
    //put your code here
    var $template = 'template';
    public function __construct() {
        parent::__construct();
    }
    public function index(){
        $data['content'] = 'admin/dashboard';
        $this->load->view($this->template,$data);
    }
    
}

model/user_model

class user_model extends CI_Model{
    //put your code here
    function __construct() {
        parent::__construct();
    }
    /*
     * function login with 
     */
    function cekLogin($username,$password){
        $this->db->select('*');
        $this->db->where('email',$username);
        $this->db->where('password',  md5($password));
        $this->db->where('status',1);
        $query = $this->db->get('users',1);
        if($query->num_rows() == 1){
            return $query->row_array();
        }
    }
}

view/template

<html>
    <head>
        <title>Inemos Server</title>
        <!-- this contain for jquery and other -->
        <link rel="stylesheet" href="<?php echo base_url() ?>/assets/css/temp.css" type="text/css" media="screen" />
        
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js"></script>
        <!-- end -->
    </head>
    <body>
        <!-- header -->
        <?php $this->load->view('header'); ?>
        
        <!-- content -->
        <?php if (!empty($content)): ?>
             <?php $this->load->view($content); ?>
        <?php else: ?>
             Halaman tidak ada
        <?php endif; ?>
        
        <!-- end content -->
        
        <!-- footer -->
        <?php $this->load->view('footer'); ?>
          
    </body>
</html>

view/admin/dashboard

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 */
echo "ini halaman dashboard";
?>

4 thoughts on “login multiuser codeigniter 2.0.2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s