<?php
error_reporting(0);
include "koneksi.php";

$user = $_POST['username'];
$pass = md5($_POST['password']);

// pastikan username dan password adalah berupa huruf atau angka.

$cek_lagi=mysqli_query($db,"SELECT * FROM tbl_user WHERE username='$user' AND pass='$pass'");
$ketemu=mysqli_num_rows($cek_lagi);
$r=mysqli_fetch_array($cek_lagi,MYSQL_ASSOC);

// Apabila username dan password ditemukan
if ($ketemu > 0){
  session_start();
   $_SESSION['kode']     = $r['id_user'];
  $_SESSION['namauser']     = $r['username'];
  $_SESSION['passuser']     = $r['pass'];
  $_SESSION['leveluser']    = $r['level_user'];
  $_SESSION['w_login']    = $r['w_login'];
  $id_user=$_SESSION['kode'];	
  
  
  if($_SESSION['leveluser']==1){
	echo "<div id='sukses' class='alert alert-info'><strong>BERHASIL...</strong><button type='button' class='close' data-dismiss='alert'><i class='ace-icon fa fa-times'></i></button></div><script>window.location ='media.php'</script>";
	mysqli_query($db,"update tbl_user set status=1,w_login=NOW() where id_user='$id_user'");
  } else if($_SESSION['leveluser']==2){
      echo "<div id='sukses' class='alert alert-info'><strong>BERHASIL...</strong><button type='button' class='close' data-dismiss='alert'><i class='ace-icon fa fa-times'></i></button></div><script>window.location ='media.php'</script>";
	mysqli_query($db,"update tbl_user set status=1,w_login=NOW() where id_user='$id_user'");
  }
}
else{

  echo "<div id='gagal' class='alert alert-danger'>Mohon maaf username & password anda salah<button type='button' class='close' data-dismiss='alert'><i class='ace-icon fa fa-times'></i></button></div>";
}

?>
