Login Di Codeigniter Futsal
*catatan
Untuk mengecek sudah login
$username = $this->session->userdata("username");
$user = $this->db->query("select * from tb_login where username='$username'")->row();
$c_user = count($user);
echo $c_user;
Tabel Login
1.id =>
2.username => varchar(100)
3.password => varchar(100)
4.role => smallint(1)
5.status => bit(1)
Autoload.php
$autoload['libraries'] = array("database","session","Form_validation");
======================================
Umum
<?php
class Auth extends CI_Controller{
public function index()
{
$this->load->view('login');
}
public function prosesLogin()
{
$this->form_validation->set_rules('username','Username','required',[
'required' => 'Username wajib diisi'
]);
$this->form_validation->set_rules('password','password','required',[
'required' => 'password wajib diisi'
]);
$u = $this->input->post('user');
$p = $this->input->post('pass');
$cp = md5($p);
$cek = $this->db->query("select * from tb_users where username='$u' and password='$cp'")->row();
$this->session->set_userdata('nama',$cek->username);
$this->session->set_userdata('role',$cek->role);
if($cek->role==1){
redirect('admin_halaman');
}
elseif($cek->role > 1){
redirect('user_halaman');
}
else{
$this->session->set_flashdata('pesan','<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-warning"></i> Username Dan Password Salah !</h4>
</div>');
redirect('Auth/index');
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('halaman/index');
}
public function test()
{
echo "bukan admin";
}
}
?>
==============================================================
Futsal
Controller
<?php
class Auth extends CI_Controller{
public function index()
{
$this->load->view('login');
}
public function prosesLogin()
{
$this->form_validation->set_rules('username','Username','required',[
'required' => 'Username wajib diisi'
]);
$this->form_validation->set_rules('password','password','required',[
'required' => 'password wajib diisi'
]);
$u = $this->input->post('user');
$p = $this->input->post('pass');
$cp = md5($p);
$cek = $this->db->query("select * from tb_login where username='$u' and password='$cp'")->row();
$cek_member = $this->db->query("select * from tb_pengguna where username='$u'")->row();
$this->session->set_userdata('status_member',$cek_member->status);
$this->session->set_userdata('username',$cek->username);
$this->session->set_userdata('role',$cek->role);
if($cek->role==1){
redirect('admin_halaman');
}
elseif($cek->role > 1){
redirect('user_halaman');
}
else{
$this->session->set_flashdata('pesan','<div class="alert alert-warning alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-warning"></i> Username Dan Password Salah !</h4>
</div>');
redirect('Auth/index');
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('auth/index');
}
public function buat_akun()
{
$nama = $this->input->post('nama');
$username = $this->input->post('username');
$password = $this->input->post('password');
$password1 = md5($password);
$jenkel = $this->input->post('jenkel');
$telephone = $this->input->post('telephone');
$alamat = $this->input->post('alamat');
$data_login = array(
'id' => '',
'username' => $username,
'password' => $password1,
'role' => 2,
'status' => 1
);
$data_pengguna = array(
'id' => '',
'nama' => $nama,
'username' => $username,
'jenkel' => $jenkel,
'alamat' => $alamat,
'telephone' => $telephone,
'photo' => 'default user1',
'status' => 0,
'saldo' => 0
);
$cek_user = $this->db->query("select * from tb_login where username ='$username'")->result();
$hasil_user = count($cek_user);
if($hasil_user>0){
$this->session->set_flashdata('pesan','<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-close"></i> Gunakan Username Lain</h4>
</div>');
redirect('Auth/index');
}else{
$this->db->insert('tb_login',$data_login);
$this->db->insert('tb_pengguna',$data_pengguna);
$this->session->set_flashdata('pesan','<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Akun Berhasil Dibuat</h4>
</div>');
redirect('Auth/index');
}
}
public function test()
{
echo "bukan admin";
}
}
?>
====================================================================
View Login.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Login</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<!-- Bootstrap 3.3.7 -->
<link rel="stylesheet" href="<?php echo base_url()?>assets/bower_components/bootstrap/dist/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="<?php echo base_url()?>assets/bower_components/font-awesome/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="<?php echo base_url()?>assets/bower_components/Ionicons/css/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="<?php echo base_url()?>assets/dist/css/AdminLTE.min.css">
<!-- iCheck -->
<link rel="stylesheet" href="<?php echo base_url()?>assets/plugins/iCheck/square/blue.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Google Font -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<style type="text/css">
</style>
<div class="backgourn" style="background: url(<?php echo base_url("Background/back1.jpg") ?>);
background-repeat:no-repeat;
background-size:cover;height:1000px;margin-top:-100px;">
<div class="batas" style="margin-bottom: 100px;height: 100px;width: 100px;background: red;">
</div>
<body class="hold-transition login-page" >
<div class="login-box" >
<div class="login-logo">
<a href="<?php echo base_url()?>assets/Login/prosesLogin.php"><b style="color:white;font-size: 50px;">Selamat Datang</b> </a>
</div>
<!-- /.login-logo -->
<div class="login-box-body" style="border-radius: 5%;">
<p class="login-box-msg"><?php echo $this->session->flashdata('pesan'); ?>
</p>
<form action="<?php echo site_url('auth/prosesLogin')?>" style="margin-bottom:15%;" method="post">
<div class="form-group has-feedback">
<input type="text" class="form-control" placeholder="Username" name="user">
<?php echo form_error('username','<div class="text-danger small ml-2">','</div');?><!--Kesalahan Form Validation-->
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" class="form-control" placeholder="Password" name="pass">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<?php echo form_error('password','<div class="text-danger small ml-2">','</div');?><!--Kesalahan Form Validation-->
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Ingatkan Saya
</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Login</button>
</div>
<!-- /.col -->
</div>
</form>
<a href="#"><i class="fa fa-question"> Lupa Password</i></a><br>
<a href="#" data-toggle="modal" data-target="#daftar" class="text-center"><i class="fa fa-user"> Buat Akun Baru</i></a>
</div>
<!-- /.login-box-body -->
</div>
<!-- /.login-box -->
<!-- jQuery 3 -->
<script src="<?php echo base_url()?>assets/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="<?php echo base_url()?>assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="<?php echo base_url()?>assets/plugins/iCheck/icheck.min.js"></script>
<script>
$(function () {
$('input').iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' /* optional */
});
});
</script>
<!--Pesan-->
<div class="modal fade" id="daftar">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><strong>Buat Akun Baru</strong></h4>
</div>
<form method="post" action="<?php echo site_url('Auth/buat_akun')?>" enctype="multipart/form-data">
<div class="modal-body">
<div class="form-group">
Nama :
<input type="text" name="nama" class="form-control">
</div>
<div class="form-group">
Username :
<input type="text" name="username" class="form-control">
</div>
<div class="form-group">
Password :
<input type="text" name="password" class="form-control">
</div>
<div class="form-group">
Jenis Kelamin :
<select name="jenkel" class="form-control">
<option value="Laki-laki">Laki-laki</option>
<option value="Perempuan">Perempuan</option>
</select>
</div>
<div class="form-group">
telephone :
<input type="text" name="telephone" class="form-control">
</div>
<div class="form-group">
Alamat :
<textarea name="alamat" class="form-control"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-close"> Batal </i></button>
<button type="submit" class="btn btn-primary"><i class="fa fa-user"> Buat Akun</i></button>
</div>
</div>
</form>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>
</body>
</html>
No comments