0

I have a problem to create permission menu,

Oke I have a table user..

username, password, permission.

I have a function in model like this..

function valid_login($username, $password)
        {
        $query = $this
            ->db
            ->where('username', $username) 
            ->where('password', md5($password)) 
            ->limit(1) 
            ->get('user'); /

        if ($query->num_rows() == 1) {
                return $query->row_array(); 
        }
        else
                {
                        return FALSE; 
                }
        }

The value post from controller, The Script like this..

public function index()
        {
              if ( isset($_SESSION['permission']) ) { 
                 redirect('home_controller/home'); 
              }

              $this->load->library('form_validation'); 
              $this->form_validation->set_rules('username', 'Username', 'required'); 
              $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
              if ( $this->form_validation->run() == TRUE ) { 
                 $this->load->model('m_user'); 
                 $result = $this->m_user->valid_login(
                             $this->input->post('username'),  
                             $this->input->post('password')
                          );

                        if ($result == TRUE) { 
                        $_SESSION['permission'] = ????;
                            redirect('home_controller/home'); 
                        }
              } 

                $this->load->view('login_form'); 
        }

I dont understand, how to get field permission value from model. I need that value to create $_SESSION..

And myView like this..

<?php
if($_SESSION['permission']=="Administrator"){
    $this->load->view('administrator_menu');;
}else{
    $this->load->view('user_menu');;
}
?>

Thanks for the attention.. Anyone can help me..

4
  • Did you start session with session_start(); on ci? CI has own session lib.
    – Bora
    Sep 11, 2013 at 10:25
  • Not yet, cause i cant get field permission value. Maybe it will if i get the value..
    – okywijaya
    Sep 11, 2013 at 10:26
  • You can use CI session like @NiloySaha answer. Or you should write into CI root /index.php this: session_start();
    – Bora
    Sep 11, 2013 at 10:29
  • why dont you use Tank Auth with groups? Much easier to use. I reccomend this adjustment :) if ($result = $this->m_user->valid_login( $this->input->post('username'), $this->input->post('password') )) {$_SESSION['permission'] = $result['permission']}
    – Kyslik
    Sep 11, 2013 at 10:50

1 Answer 1

1

Like this to set session in CI:

if ($result == TRUE) { 
    print_r( $result );die;
    //$this->sesson->set_userdata('logged_user', $result); #this to set the session with the returned result
    $_SESSION['permission'] = $result['permission'];  #set permission from the result in the session
    redirect('home_controller/home'); 
}

Now to read the session if exists or not do like this:

if( $this->session->userdata('logged_user') ){
    echo "The user id stored in session is :".$id = $this->session->userdata['logged_user']['id'];
}else{
    redirect('someplace', 'refresh');
}

EDIT :
To start the session first autoload it in application/autoload.php:

$autoload['libraries'] = array('session');

Secondly set a random encryption key in application/config.php like this:

$config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
8
  • Sorry Niloy, I still not understand with your script. I just want get permission field, because i want to create multiple level user..
    – okywijaya
    Sep 11, 2013 at 10:35
  • is permission an attribute in your table user? @okywijaya
    – Nil'z
    Sep 11, 2013 at 10:37
  • I think the error in query $query = $this ->db ->where('username', $username) ->where('password', md5($password)) ->limit(1) ->get('user'); i not yet declare field permission..
    – okywijaya
    Sep 11, 2013 at 12:09
  • Print the result once to see what is returned.
    – Nil'z
    Sep 11, 2013 at 12:09
  • I insert your script if ($query->num_rows() == 1) { return $query->row_array();$_SESSION['permission'] = $result['permission']; I get error like this.. A PHP Error was encountered Severity: Notice Message: Undefined index: permission Filename: views/home.php
    – okywijaya
    Sep 11, 2013 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.