当前位置: 动力学知识库 > 问答 > 编程问答 >

codeigniter - how to redirect user depending on user type at time of login (codeignitor)

问题描述:

im facing problem while redirecting my user according to its type. how can do it here's my code plz suggest how to do it.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VerifyLogin extends CI_Controller {

function __construct()

{

parent::__construct();

}

function index()

{

$this->load->model('user','',TRUE);

//This method will have the credentials validation

$this->load->library('form_validation');

$this->load->library('session');

$this->form_validation->set_rules('username', 'Username','trim|required|xss_clean');

$this->form_validation->set_rules('password', 'Password'

'trim|required|xss_clean|callback_check_database');

if($this->form_validation->run() == FALSE)

{

//Field validation failed.&nbsp; User redirected to login page

validation_errors();

$this->load->view('error');

}

else

{

//Go to private area

basically here i want to redirect if user is admin then redirect to admin page else redirect to home how can i do this ???

 redirect('home', 'refresh');

}

}

function check_database($password)

{

//Field validation succeeded.&nbsp; Validate against database

$username = $this->input->post('username');

//query the database

$result = $this->user->login($username, $password);

if($result)

{

$sess_array = array();

foreach($result as $row)

{

$sess_array = array(

'id' => $row->id,

'username' => $row->username

);

$this->session->set_userdata('logged_in', $sess_array);

}

return TRUE;

}

else

{

$this->form_validation->set_message('check_database', 'Invalid username or password');

return false;

}

}

}

?>

网友答案:

The way I do this is by joining the user with a roles table. Each user is assigned to a role (such as salesperson, accounting manager, etc.). Each role has an optional home page to redirect to after login. If it's not set, it redirects to a default page.

网友答案:

Okay, well you say you have a login function so here are the basics. What Mike is saying is right and how I do it, but if you only have two types of users a roles solution is probably overkill. Personally I use the roles to populate a user menu, all users get the same menu, the options change depending on what portions of the site they're allowed to see.

For a basic admin/user though that's really not necessary. What you need to do is just redirect based on usergroup after login, so something similar to this.

$this->db->where(username, $username);
$this->db->where(password, $password);
$query = $this->db->get(users);
if($query->num_results() ==1)
{
    $result = $query->result_array();
    switch ($result['usergroup']) {
        case '1':
        redirect 'home';
        break;
        case '2':
        redirect 'admin';
        break;
        default:
        redirect 'home';
        break;
    }
}

As I said this is a basic solution that you can use for a few different roles, if you want to do anything more complicated investigate creating roles.

You'll also want to save the usergroup to the session and check it on admin pages, if the user isn't an admin redirect them away from the page.

分享给朋友:
您可能感兴趣的文章:
随机阅读: