<?php

	$xfdc = "";

setcookie("unidx", "", time() - 3600);
$cookie_value1 = uniqid().date("YmdHis");
setcookie("unidx", $cookie_value1, time() + (86400 * 30),"/",NULL, FALSE,TRUE); // 86400 = 1 day

		// SESSION ID
		$xfdc 	= $cookie_value1;

?>

<?php

include 'db_connect.php';
include 'functions.php';
sec_session_start(); // Our custom secure way of starting a php session. 
 
if(isset($_POST['email'], $_POST['p'])) { 
   $email = $_POST['email'];
   $password = $_POST['p']; // The hashed password.
   if(login($email, $password, $mysqli) == true) {
      // Login success

	  // ADDED FROM BYTE12 TO GET CORRECT username
		// Create connection
		$conn = mysqli_connect($data_server, $data_user, $data_pswd, $data_db);
		// Check connection
			if (!$conn) {
			die("Connection failed: " . mysqli_connect_error());
			}
		
		$sql = "select username from members where email = '".$email."'";
		$result = mysqli_query($conn, $sql);
		if (mysqli_num_rows($result) > 0) {
			while($row = mysqli_fetch_assoc($result)) {
					$username_rabio = $row["username"];
			}
			}
		mysqli_close($conn);


		// GET CURRENT DIRECTORY AND ADD USERNAME
		$currentWebDir = $currentWebDir = str_replace('\\', '/', substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT'])));
		$currentWebDir = substr($currentWebDir,0,-11);	  
		// echo $currentWebDir;
		$locat = "Location: ".$currentWebDir."projects.php?xuid=".$username_rabio."&xfdc=".$xfdc;
		// echo $locat;				
		
		
     header($locat);
   } else {
      // Login failed
     header("Location: '..\..\..\?error=1");

   }
} else { 
   // The correct POST variables were not sent to this page.
   echo 'Invalid Request';
}




;?>