Welcome Guest, Not a member yet? Register   Sign In
Login two users from one login form
#1

[eluser]shahmy[/eluser]
Hi, I have two tables,
1.students
2.teachers
and In my view i was created one login form with username and password.
my problem is,
when the student log to the site i want redirect that person to student panel and if teacher log to the site i want redirect that person to teachers panel.
I was don this, But I cant redirect the student to student panel,
this is my code:

"The Login Controller"
function validate_users()
{

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');

if ($this->form_validation->run() == FALSE)
{
$this->index();
}

else
{

$this->load->model('student_model');
$query = $this->student_model->validate();


if($query) // if the user's credentials validated...
{
$data = array(
//'tech_id'=>$tech_id,
'username' => $this->input->post('username'),
'is_logged_in' => true
);

/*foreach ($query->result() as $row)
{
$row->tech_id;
}

$tech_id=$this->session->set_userdata('tech_id');*/
//$data['query'] = $this->teachers_model->getLoginId();
//$this->session->set_userdata($data);
$this->session->set_userdata($data);
redirect('main/student/memberarea');
}

/*else
{

$this->load->model('teachers_model');
$data = $this->teachers_model->general();
$data['error']='The Information Not In Database';
$this->load->view('main/index',$data);
//$this->index();

}*/


else

{
$this->load->model('teachers_model');
$query = $this->teachers_model->validate();

if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('main/teachers/memberarea');
}


else
{

$this->load->model('teachers_model');
$data = $this->teachers_model->general();
$data['error']='The Information Not In Database';
$this->load->view('main/index',$data);
//$this->index();

}


}

}

}

please help me.
Thank You.
#2

[eluser]WanWizard[/eluser]
Most obvious question is: why do you make this so complex by putting two types of users in two different tables?

Put them in one table, and use a column to indicate which type of user it is. That keeps your login simple, and you can redirect based on the user type.
You need this anyway, because I assume you also want to make sure students can't access main/teachers/memberarea, if they type in the URL directly? So you need a flag in the session to indicate the user type anyway.
#3

[eluser]crikey[/eluser]
@WanWizard,

That's a generalisation and might not work for the OP's application. Maybe the different user types have vastly different data structures? In which case separate tables would be fine, in fact, preferred. And a session variable could still indicate which type of user is logged in - whether you use distinct tables or the same table.

@shahmy: load both student and teacher models after your form_validation check (validation check returns false - do your thing) or (validation returns true)... then do if/elseif checks... for student, get their data and set a session variable for the currently logged in user, which includes indicating their type... for teacher, do same as student (but indicate their type as 'teacher' in the session variable).

Cheers,
Grant
#4

[eluser]gyo[/eluser]
I do agree with WanWizard, as a simpler approach only brings benefits.
IMO students and teachers are users with basic data (username, password), and each one can have a different "profile" table (students_profile, teachers_profile) with more data.

If you have two separated tables for users, you need to check against both tables if the username provided belongs to a student or a teacher, before actually trying to login.
#5

[eluser]WanWizard[/eluser]
@crikey:

that's why I started with a question. A "there are reasons" would suffice as an answer.

In the case of multiple tables, I would opt to hide the complexity in the model, with a single login() and is_logged_in() method, in which you make the descisions on which table to check. Then normalize the results returned by the model, so the controller calling the model is agnostic to which table was used, and you have a flexible solution that doesn't require your controllers to have knowledge of which authentication method was used.

I use the same system for logins via local db, Active Directory, LDAP, OpenID or a social network. The application only needs be able to check if a user is logged in, and if this user has the authorisation to perform a given task.




Theme © iAndrew 2016 - Forum software by © MyBB