<?php



$username = $_POST['user_name_text'];
$password = $_POST['password_text'];

// Include database connection settings
//include("db_connect.php");

$con = mysql_connect("localhost","custsoft","wapulannunuka");
if (!$con)
  {
  die('Could not connect to database: ' . mysql_error());
  }
   
mysql_select_db("custsoft", $con);

function execsql($qry){
	$result = mysql_query($qry);
	return $result;
}

// Retrieve username and password from database according to user's input


//$login_query = "SELECT  username   FROM ps_users WHERE  (username= '".$username."' AND (password= '".(md5($password))."')";
//$login_result = mySQL_query($login_query);
//$login_num = mysql_num_rows($login_result);
//echo $login_query;
//echo "<br>number:".$login_num;


$login_query = "SELECT username FROM ps_users WHERE (username = '" . $username . "') and (password = '" . (md5($password)) . "')";
echo $login_query;
$login_result = mySQL_query($login_query);

$num = mysql_num_rows($login_result);
echo "num:".$num;
// Check username and password match
if ($num == 1) {
// Inialize session
session_start();	
// Set username session variable

$access = "SELECT access_id, home_page FROM ps_users WHERE (username = '" .$username. "') ";
//echo $access;
$accessResult = mySQL_query($access);
$_SESSION['access_id'] = mysql_result($accessResult, 0, "access_id");
$home_page = mysql_result($accessResult, 0, "home_page");
$_SESSION['username'] = $_POST['user_name_text'];
//echo "home:".$home_page;
date_default_timezone_set('Africa/Khartoum');
$entry_date = date("Y-m-d H:i:s",time()-3600); 

$active_query = "INSERT INTO ps_active_users (username, log_in) VALUES ('".$_SESSION['username']."', '".$entry_date."' ) ";
execsql($active_query);
//echo $active_query."<br>";

$table_name = "ps_tracking_log_in";
$action_query = "INSERT INTO $table_name (entry_date, user, action) VALUES ('".$entry_date."' ,'".$_SESSION['username']."','log in' )";
execsql($action_query);


if ($home_page=="HR") {$location = "../hr_module/hr_main.php?period=day"; }
if ($home_page=="Hire") {$location = "../hire/hire_main.php?period=day"; }
if ($home_page=="Transport") {$location = "../transport/transport_main.php?period=day"; }
if ($home_page=="Admin") {$location = "../admin_module/user_list.php"; }
if ($home_page=="") {$location = "../login.php?e=2"; }

echo $location;

// Jump to secured page
  header('Location: '.$location );
}
else {
//Jump to login page
    header('Location: ../login.php?e=1');
}

?>