yogieputra
yogieputra

Reputation: 647

How to redirect to another controller in CodeIgniter?

I am working on new project with CodeIgniter. Currently Im stuck in login module. My code and DB schema looks like this:

And this is my Database Schema (DB: Membership, Table: User):

---------------------------------------------------------------------------------------------------------------------------------
id   | nama_depan   | nama_belakang |   username |  password                        |   email_address       |   jabatan         |
-----|--------------|---------------|------------|----------------------------------|-----------------------|-------------------|
2    | Joe          | Sambera       |   sambera  | e10adc3949ba59abbe56e057f20f883e |   [email protected]   |   Manajemen       |
10   | holmes       | Richard       |   holmes   | 81dc9bdb52d04dc20036dbd8313ed055 |   [email protected]    |   Event Organizer |
---------------------------------------------------------------------------------------------------------------------------------

Here's my model (membership_model):

<?php

class Membership_model extends CI_Model {

function validate()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('jabatan', $this->input->post('jabatan'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('membership');

    if($query->num_rows == 1)
    {
        return true;
    }

}

function create_member()
{

    $new_member_insert_data = array(
        'nama_depan' => $this->input->post('first_name'),
        'nama_belakang' => $this->input->post('last_name'),
        'email_address' => $this->input->post('email_address'),         
        'username' => $this->input->post('username'),
        'jabatan' => $this->input->post('jabatan'),
        'password' => md5($this->input->post('password'))
        //'jabatan' => $this->input->post('jabatan'))                       
    );

    $insert = $this->db->insert('membership', $new_member_insert_data);
    return $insert;
}
}

Here's my Views (Login):

<?php $this->load->view('includes/header'); ?>  

<h1>Login</h1>
<?php 
echo form_open('login/validate_credentials');
echo form_input('username', 'Username');
echo form_password('password', 'Password');
echo form_submit('submit', 'Login');
//echo anchor('login/signup', 'Create Account');
echo form_close();
?>

<?php $this->load->view('includes/footer'); ?>

Here's my Controller (login):

<?php

class Login extends CI_Controller {

    function index()
{
    $data['main_content'] = 'login_form';
    $this->load->view('includes/template', $data);      
}

function validate_credentials()
{       
    $this->load->model('membership_model');
    $query = $this->membership_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('site/members_area');

        if($jabatan == "Manajemen"){
            redirect('site/manajemen_page');
        }
        else{
            redirect('site/eo_page');
        }
    }
    else // incorrect username or password
    {
        $this->index();
    }
}   

function signup()
{
    //$data['main_content'] = 'signup_form';
    //$this->load->view('includes/template', $data);
    $this->session->set_userdata($data);
    redirect('site/signup_page');
} 
}

Here's the another Controller (site):

<?php

class Site extends CI_Controller 
{
    function __construct()
    {
        parent::__construct();
        $this->is_logged_in();
    }

function members_area()
{
    $this->load->view('logged_in_area');
}

function signup_page() // signup form
{ 
    $this->load->view('signup_form');
}

function manajemen_page() // just for Manajemen
{
    $this->load->view('manajemen_page');
}
function eo_page() // just for EO
{
    $this->load->view('eo_page');
}

function another_page() // just for sample
{
    echo 'good. you\'re logged in.'; 
}

function is_logged_in()
{
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true)
    {
        echo '<br><br><br><br><br><br><br>';
        echo '<strong>';
        echo '<center>You don\'t have permission to access this page. <a href="../login">Login</a></center>';   
        echo '</strong>';
        die();      
        //$this->load->view('login_form');
    }       
}   
}

As you can see in Controller/Login, in this below code I want to try to authenticate the user who login. If Managament who login the sites will be redirect to manajemen_page in controller/site. And if Event Organizer who login, the site will be redirected to EO_page in controller/site. (my code below is not working)

if($jabatan == "Manajemen"){
            redirect('site/manajemen_page');
        }
        else{
            redirect('site/eo_page');
        }

Can you guys help me with this?

Upvotes: 1

Views: 5377

Answers (1)

CodeGodie
CodeGodie

Reputation: 12132

Ok you missed a couple of things here. First, you didn't return the results from your model, thus your variable $jabatan is undefined. Second, your model should not have any input->post() in it, postdata should be handled only in your controller.

Your controller function validate_credentials should be re-written like this:

function validate_credentials() {
    $this->load->model('membership_model');
    $postdata = $this->input->post();
    //you can run any checks or cleaning you need to do to the postdata your passed in, then send it to model
    $result = $this->membership_model->validate($postdata);
    if ($result) { // if the user's credentials validated...
      $data = array(
          'username' => $postdata['username'],
          'is_logged_in' => true
      );
      $this->session->set_userdata($data);
      //redirect('site/members_area');

      if ($result->jabatan === "Manajemen") { //CHANGE
        redirect('site/manajemen_page');
      } else {
        redirect('site/eo_page');
      }
    } else { // incorrect username or password
      $this->index();
    }
}

and your model function validate:

function validate($data) {
    $this->db->select("*");
    $this->db->from("user");
    $this->db->where('username', $data['username']);
    $this->db->where('password', md5($data['password']));
    $query = $this->db->get();

    if ($query->num_rows == 1) {
      return $query->row();
    }else{ 
      return false;
    }
}

Upvotes: 1

Related Questions