<?php
	session_start();

	require_once("config/mysql_connect.php");
	
	$strUsername = mysqli_real_escape_string($link,$_POST['username']);
	$strPassword = mysqli_real_escape_string($link,$_POST['password']);
	
	$salt = 'fq7wrt3ygio8p5axd4h9j0kl2zcevb1nm6su';
	$hash_pwd = hash_hmac('sha256', $strPassword, $salt);

	$strSQL = "SELECT * FROM tb_account WHERE acc_user = '".$strUsername."' 
	and acc_pass = '".$hash_pwd."'";
	$objQuery = mysqli_query($link,$strSQL);
	$objResult = mysqli_fetch_array($objQuery);
	if(!$objResult)
	{
		header("location:login.php");
		exit();
	}
	else
	{
			//*** Update Status Login
			$sql = "UPDATE tb_account SET acc_lastupdate = NOW() WHERE acc_id = '".$objResult["acc_id"]."' ";
			$query = mysqli_query($link,$sql);

			//*** Session
			$_SESSION["ses_acc_id"] = $objResult["acc_id"];
			$_SESSION["ses_acc_user"] = $objResult["acc_user"];
			$_SESSION["ses_acc_status"] = $objResult["acc_status"];
			session_write_close();

			//*** Go to Main page

			if($objResult["acc_status"] == 'admin'){
				header("location:admin/announce.php");
			}else{
				header("location:main/index.php");
			}
			
	}
	mysqli_close($link);
?>