Welcome Guest, Not a member yet? Register   Sign In
checking usergroup on CI
#1

[eluser]Amir Rachman[/eluser]
hi all, i wanna ask about usergroup using CI, on PHP structural code, i register the session on login process then i can check the register session group

Code:
<?php if ($_SESSION[usergroup] == 'Administrator' { ?>  
    <td width="50" style="text-align:center"><a href="update.php">Update</a></td>
&lt;?php }  ?&gt;

some kind like that, if the session usergroup checking on that page is register by "administrator" session usergroup the "update" link will be available, and if the session register on "user" session, update links will be appear. how implementing this code on CI.

now i've create login process somekind like this

Code:
function process_login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == TRUE)
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            if ($this->modelLogin->check_user($username, $password) == TRUE)
            {
                $data = array('username' => $username, 'login' => TRUE);
                $this->session->set_userdata($data);
                redirect('passport');
            }
            else
            {
                $this->session->set_flashdata('message', 'Invalid Username or Password');
                redirect('login');
            }
        }
        else
        {
            $this->load->view('login/viewLogin');
        }
    }

how to modified that code to be checking usergroup session ?
#2

[eluser]K-Fella[/eluser]
Is this what you mean?

Code:
&lt;?php if ($this->session->userdata('usergroup') == 'administrator' { ?&gt;
<td width="50" style="text-align:center"><a href="update.php">Update</a></td>
&lt;?php } ?&gt;
#3

[eluser]Amir Rachman[/eluser]
[quote author="K-Fella" date="1288553249"]Is this what you mean?

Code:
&lt;?php if ($this->session->userdata('usergroup') == 'administrator' { ?&gt;
<td width="50" style="text-align:center"><a href="update.php">Update</a></td>
&lt;?php } ?&gt;
[/quote]

then how to check on mysql table,
example :

username : admin
password : admin
usergroup : Administrator

i want to check if user is admin then he have usergroup access as a administrator, so i must moddified the process login code that i gave to this post.
#4

[eluser]smilie[/eluser]
Actually, you have to modify your Model.
Instead of:

if ($this->modelLogin->check_user($username, $password) == TRUE)

You will have to do something like:
$check = $this->modelLogin->check_user($username, $password);

And in model, you will have to query for usergroup as well. You could then return (from model)

$result['login'] = TRUE / FALSE;
$result['usergroup'] = $dbresult['usergroup'];

Hope this helps.

Regards,
Smilie
#5

[eluser]Amir Rachman[/eluser]
hmm i'm confuse about the explanation, let me paste the code in here.

1. this is my model

Code:
&lt;?php
class ModelLogin extends Model {
    function __construct()
    {
        parent::Model();
    }
    
    var $table = 'user';
    
    /**
     * Cek tabel user, apakah ada user dengan username dan password tertentu
     */
    function check_user($username, $password)
    {
        $query = $this->db->get_where($this->table, array('username' => $username, 'password' => md5($password)), 1, 0);
        
        if ($query->num_rows() > 0)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}
?&gt;

2. this is my controller

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller {
    function __construct()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('table');
        
        $this->load->model('modelLogin', '', TRUE);
    }
    
    /**
     * Memeriksa user state, jika dalam keadaan login akan menampilkan halaman absen,
     * jika tidak akan meload halaman login
     */
    function index()
    {
        if ($this->session->userdata('login') == TRUE)
        {
            redirect('passport');
        }
        else
        {
            $this->load->view('login/viewLogin');
        }
    }
    
    /**
     * Memproses login
     */
    function process_login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == TRUE)
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            if ($this->modelLogin->check_user($username, $password) == TRUE)
            {
                $data = array('username' => $username, 'login' => TRUE);
                $this->session->set_userdata($data);
                redirect('passport');
            }
            else
            {
                $this->session->set_flashdata('message', 'Invalid Username or Password');
                redirect('login');
            }
        }
        else
        {
            $this->load->view('login/viewLogin');
        }
    }
    
    /**
     * Memproses logout
     */
    function logout()
    {
        $this->session->sess_destroy();
        redirect('login', 'refresh');
    }
}
// END Login Class

/* End of file login.php */
/* Location: ./system/application/controllers/login.php */
?&gt;

my question is, how to register session usergroup on mysql table using CI, my structural code login, will be like this code :

Code:
&lt;?
    session_name('panel');
    session_start();
    include "db.php";
    
    connect();
    $Pass = md5($_POST[password]);
    
    
    $sql = mysql_query("select * from tbl_user where username='$_POST[username]' and password = '$Pass'");
    $res = mysql_fetch_array($sql);
    
    if($res) {
        session_register("Id");
        session_register("username");
        session_register("password");
        session_register("usergroup");
        $_SESSION[Id] = $res["Id"];
        $_SESSION[username] = $res["username"];
        $_SESSION[password] = $res["password"];
        $_SESSION[usergroup] = $res["usergroup"];
        
        header("location:index2.php");
    }
    else {
        echo "[removed]alert('Incorrect Username or Password, Please Try Again'); document.location.href='login.php';[removed]\n";
    }
?&gt;

i'm register the usergroup data, usergroup data from mysql DB. now how implement this to CI. help me Sad
#6

[eluser]smilie[/eluser]
Well, first of all - I presume you have 'usergroup' in your database?

If no - create one :-)
If yes (which makes more logic), you would simply do this:

In your model:
Code:
&lt;?php
class ModelLogin extends Model {
    function __construct()
    {
        parent::Model();
    }
    
    var $table = 'user';
    
    /**
     * Cek tabel user, apakah ada user dengan username dan password tertentu
     */
    function check_user($username, $password)
    {
        $query = $this->db->get_where($this->table, array('username' => $username, 'password' => md5($password)), 1, 0);

        # Store result from database in a variable
        $result = $query->result_array();
        
        if ($query->num_rows() > 0)
        {
            return $result;
        }
        else
        {
            return FALSE;
        }
    }
}
?&gt;

Now, your model will send 'usergroup' from the query to the controller.

So, in controller:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Controller {
    function __construct()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('table');
        
        $this->load->model('modelLogin', '', TRUE);
    }
    
    /**
     * Memeriksa user state, jika dalam keadaan login akan menampilkan halaman absen,
     * jika tidak akan meload halaman login
     */
    function index()
    {
        if ($this->session->userdata('login') == TRUE)
        {
            redirect('passport');
        }
        else
        {
            $this->load->view('login/viewLogin');
        }
    }
    
    /**
     * Memproses login
     */
    function process_login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        
        if ($this->form_validation->run() == TRUE)
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            if ($check_login = $this->modelLogin->check_user($username, $password))
            {
                $data = array('username' => $username, 'login' => TRUE, 'usergroup' => $check_login['usergroup']);
                $this->session->set_userdata($data);
                redirect('passport');
            }
            else
            {
                $this->session->set_flashdata('message', 'Invalid Username or Password');
                redirect('login');
            }
        }
        else
        {
            $this->load->view('login/viewLogin');
        }
    }
    
    /**
     * Memproses logout
     */
    function logout()
    {
        $this->session->sess_destroy();
        redirect('login', 'refresh');
    }
}
// END Login Class

/* End of file login.php */
/* Location: ./system/application/controllers/login.php */
?&gt;

So, in model - you collect usergroup from database;
In your controller, you set in session->save_userdata usergroup with value from the database.

Cheers,
Smilie
#7

[eluser]Amir Rachman[/eluser]
thank's for smilie, now i've got that i want. the value returning is usergroup based on logged user in database.




Theme © iAndrew 2016 - Forum software by © MyBB