CodeIgniter Forums
how to using session in library...please help me... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: how to using session in library...please help me... (/thread-17915.html)



how to using session in library...please help me... - El Forum - 04-20-2009

[eluser]Unknown[/eluser]
i was creating a web site for my school and for the login menu i have make like this:

model:
<?php
class Login_m extends Model
{
function __construct()
{
}

function login($ID, $password)
{
$this->db->where('ID_user', $ID);
$this->db->where('password', $password);
$dat = $this->db->get('user');
if($dat->num_rows() > 0)
{
return 'OK';
}
else
{
return 'FAILED';
}
}
}

controller:
<?php

class login_c extends Controller
{
function __construct()
{
parent::controller();
}
function index()
{
$this->load->view('public\login');
}
function login_process()
{
$ID = $this->input->post('ID');
$password = $this->input->post('password');
$this->load->model('login_m');
if($this->login_m->login($ID, $password)== 'OK')
{
echo $this->load->view('public\siswa'); }
else
{
echo $this->load->view('public\login2'); }

}
function _success()
{
$this->load->view('public\home_view');
}
}
?>


and now i can't develop my project bcoz i dont know to complete the login menu
i have created table user with field : ID_user, Name_user, Level, password...
user access with their level, if level 1 for admin, level 2 for teacher, level 3 for the student..and if the login is failed the page will showing error message "login failed"
what the code so i can login with the level i was created, and every page for every user just can access if the user was login(restrict page)...
please give me the source code for controller and the model, and if there are any change in the library please explain for me about it..and what can i do with every page from every user..

thanks....


how to using session in library...please help me... - El Forum - 04-20-2009

[eluser]Dam1an[/eluser]
Firtly, you shoould always use code blocks when you have more then one or two lines of code, I had to paste it in my IDE to be able to easily read your code

Here's some quick code I knocked up

Make sure you autoload session and database

Model:
Code:
<?php
class Login_model extends Model {
    function __construct() {
        // you forgot to call the parent constructor
        parent::Model();
    }
    
    function login($id, $password) {
        $this->db->where('id', $id);
        $this->db->where('password', $password);
        
        return $this->db->get('users');
    }
}
?>

Controller:
Code:
<?php
class Login extends Controller {
    function __construct() {
        parent::Controller();
        $this->load->model('login_model');
    }
    
    function index() {
        $this->load->view('login');
    }
    
    function _process_login() {
        $id = $this->input->post('id');
        $password = $this->input->post('password');
        $query = $this->login->login($id, $password);
        
        if($query->num_rows() == 0) {
            ... failed to login logic here ...
        } else {
            $user = $query->row();
            
            // set the user level in the session
            $this->session->set_userdata('level', $user->level);
            
            ... do the rest of you successful login stuff ...
        }
    }
}
?>

As for only letting you view pages you have the access level to view, just put a check like
Code:
if($this->session->userdata('level') == 1) {
    ... Have correct level to view page ...
} else {
    ... Can't access this page ...
}