MWI – Buat file baru dengan nama Auth.php letakan dalam folder application/controllers dan isi dengan
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function login(){
check_already_login();
$this->load->view('login');
}
public function process(){
$post = $this->input->post(null, TRUE);
if(isset($post['login'])){
$this->load->model('UserModel');
$query = $this->UserModel->login($post);
if($query->num_rows()>0){
$row = $query->row();
if($row->foto==NULL){
if($row->gender=="L"){
$foto="0m.png";
}else{
$foto="0f.png";
}
}else{
$foto=$row->foto;
}
$params = array(
'posuserid' => $row->id,
'poslevel' => $row->level,
'posusername' => $row->username,
'posnama' => $row->nama,
'posgender' => $row->gender,
'posemail' => $row->email,
'posfoto' => $foto,
);
$this->session->set_userdata($params);
redirect('dashboard');
/* echo "<script>
alert('Selamat, login berhasil');
window.location='".site_url('dashboard')."';
</script>"; */
}else{
echo "<script>
alert('Maaf ada yang salah');
window.location='".site_url('auth/login')."';
</script>";
}
}else{
echo "tidak dicek login-nya";
}
}
public function logout(){
$params=array('posuserid', 'poslevel', 'posusername', 'posnama', 'posemail', 'posfoto');
$this->session->unset_userdata($params);
redirect('auth/login');
}
}
Buat file UserModel.php letakan pada folder application/models dan isi dengan code (sementara jenis hash yang digunakan sha1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class UserModel extends CI_Model{
public function login($post){
$this->db->select('*');
$this->db->from('user');
$this->db->where('username', $post['username']);
$this->db->where('password', sha1($post['password']));
$query = $this->db->get();
return $query;
}
public function get($id=null){
$this->db->from('user');
if($id != null){
$this->db->where('id', $id);
}
$query = $this->db->get();
return $query;
}
}
Demikian tutorial ini dibuat, semoga menjadi assets dan ibadah bagi kita. Aamiin. (ms).
Pingback: Daftar Isi Aplikasi POS dengan CodeIgniter | Muhidin Saimin