Reputation: 59
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..
Upvotes: 0
Views: 1206
Reputation: 7475
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';
Upvotes: 1