<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/
ob_start();
session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>

<html>

<?php include 'head.php'; ?>

<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>	

	<fieldset style="width:85%"><legend>HELP</legend>
	<p>If you have an account you can log-in here.</p> 
	<p>Or you can sign-up with the link at the bottom of page.</p>
	</fieldset><br>
</div>

<div id="content">

<?php

// define variables and set to empty values
$userErr = $passwordErr = "";
$user = $password = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
  if (empty($_POST["user"]))
    {$userErr = "(User is required)";}
  else
    {$user = test_input($_POST["user"]);}
	
  if (empty($_POST["password"]))
    {$passwordErr = "(Password is required)";}
  else
    {$password = test_input($_POST["password"]);}
	  
}

function test_input($data)
{
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}

//if the form fields filled in
if ($user != "" AND $password != "")
{

$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}


mysql_select_db("university", $con);

$sql="SELECT * FROM users WHERE UserName='$user' AND Pass='$password' ";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

//get user number from database
$result = mysql_query("SELECT * FROM users WHERE UserName='$user' AND Pass='$password' ");
while ($row = mysql_fetch_array($result))
{
$UserID=$row['UserID'];
}

// If result matched $user and $password, table row must be 1 row
if($count==1)
{
$_SESSION['loggedin'] = $UserID;
}
else 
{
echo "<br>";
echo "<h2>Wrong Username or Password</h2>";
echo "<h2>Please try again !!!!</h2><hr/>";
}

//close the connection to the database.
mysql_close($con);
}

?>

<br>
<fieldset style="width:50%"><legend>LOG-IN HERE</legend>
<?php
if ($_SESSION['loggedin'] > 0)
	{
		header('Location: index.php');
	}
ob_end_flush()
?>

<!-- user log-in form -->
<p><span class="error">* required fields</span></p>
<form  method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

User: <span class="error">*</span><span class="error"> <?php echo $userErr;?> </span>
<br><input type="text" name="user" size="40" value="<?php echo $user; ?>"><br><br>

Password: <span class="error">*</span><span class="error"> <?php echo $passwordErr;?> </span>
<br><input type="password" name="password" size="40"><br><br>

<input id="button" type="submit" name="submit" value="Log-In">
</form><br><hr/>

<p>* if you forgotten your password we can send a reminder for you.
<a href='password-reminder.php'>Password reminder</a></p>

</fieldset>
<br/><hr/><br />

<!-- navigation link -->
<p>* or if you haven't got an account yet, 
<a href='sign-up-form.php'>Sign-up</a></p>

</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>
	

</div>
</body>
</html>
