• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login from Outlet and Admin Table

#1
Hello All... I am Newbie in CodeIgniter

I want to make my own website for my company and I have a problem in making a Login from 2 database table.


My Controller :



Quote:   public function process()
   {
       $username = $this->input->post('username');
       $password = $this->input->post('password');

       $staff = $this->login_m->login_staff($username, $password);
       if ($staff->num_rows() > 0) {
           $data = $staff->row_array();
           $this->session->set_userdata('login', TRUE);
           if ($data['level'] == '1') {
               $this->session->set_userdata('akses', '1');
               $this->session->set_userdata('ses_id', $data['username']);
               $this->session->set_userdata('ses-name', $data['name']);
               redirect('dashboard');
           } else if ($data['level'] == '2') {
               $this->session->set_userdata('akses', '2');
               $this->session->set_userdata('ses_id', $data['username']);
               $this->session->set_userdata('ses-name', $data['name']);
               redirect('dashboard');
           } else if ($data['level'] == '3') {
               $this->session->set_userdata('akses', '3');
               $this->session->set_userdata('ses_id', $data['username']);
               $this->session->set_userdata('ses-name', $data['name']);
               redirect('dashboard');
           } else if ($data['level'] == '4') {
               $this->session->set_userdata('akses', '4');
               $this->session->set_userdata('ses_id', $data['username']);
               $this->session->set_userdata('ses-name', $data['name']);
               redirect('dashboard');
           }
       } else {
           $cust = $this->login_m->login_cust($username, $password);
           if ($cust->num_rows() > 0) {
               $data = $cust->row_array();
               $this->session->set_userdata('login', TRUE);
               $this->session->set_userdata('akses', '5');
               $this->session->set_userdata('ses_id', $data['username']);
               $this->session->set_userdata('ses_name', $data['name']);
               redirect('dashboard');
           } else {
               $this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Username/Password are Wrong</div>');
               redirect('v_login');
           }
       }


Here is My Model :

Quote:public function login_staff($username, $password)
   {
       $query = $this->db->query("SELECT * FROM manajemen WHERE handphone='$username' AND password=MD5('$password') LIMIT 1");
       return $query;
   }

   public function login_cust($username, $password)
   {
       $query = $this->db->query("SELECT * FROM customer WHERE handphone='$username' AND password=MD5('$password') LIMIT 1");
       return $query;
   }


When I try this code, the page dont show any error,...just return to the login page.
please help me solve this problem.
tq
Reply

#2
Try one of the available authentication libraries for CodeIgniter, like "Ion_auth" or "Community_auth". It will save a lot of time, and you will have better security.
Besides that, learn how to avoid repeating code.
E.g. the part where you determine if the user is a staff member or a customer can be way shorter:

PHP Code:
if ($staff->num_rows() > 0) {
 
 $data $staff->row_array();
 
 $this->session->set_userdata('login'TRUE);
 
 $this->session->set_userdata('akses',$data['level');
 
 $this->session->set_userdata('ses_id'$data['username']);
 
 $this->session->set_userdata('ses-name'$data['name']);
 
 redirect('dashboard');
}
else {
     ...

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.