<?php
session_start();

//	Go to index page if session exist
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true)
{
	header('Location: rotator.php');
	die();
}

//	If set to true, it shows general error
$error = false;

//	Check if form was submitted, if so, check username and password
//	which are hardcoded in this case
if (isset($_POST['login']) && $_POST['login'] == 'true')
{
	//	Here you can set the username and password that are required for login
	$username = 'AmZn-biz0!';
	$password = '27e5393a9c5d050b7e40ff3689839ca8';
	
	//	Save data from form
	$form['username'] = isset($_POST['username']) ? trim($_POST['username']) : '';
	$form['password'] = isset($_POST['password']) ? trim($_POST['password']) : '';

	//	Check if username matches
	if ($form['username'] === '' || $form['username'] !== $username)
	{
		$error = true;
	}
	
	//	Check if password matches
	if ($form['password'] === '' || MD5($form['password']) !== $password)
	{
		$error = true;
	}
	
	if ($error == false)
	{
		//	No error found so let's set the session and
		//	redirect to rotator.php
		$_SESSION['logged_in'] = true;
		
		header('Location: rotator.php');
		die();
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Login</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
 
<body>
	<div id="page">
		<h1>Login</h1>
		<?php
		//	Show login error
		if ($error == true)
		{
		?>
		<p class="error">Login failed!</p>
		<?php
		}
		?>
		<form action="login.php" method="post" id="login-form">
			<div>
				<input type="hidden" name="login" value="true" />
			</div>
			<label for="username">
				<input type="text" name="username" id="username" placeholder="Username" value="" />
			</label>
			<label for="password">
				<input type="password" name="password" id="password" placeholder="Password" value="" />
			</label>
			<label for="submit">
				<input type="submit" name="submit" id="submit" value="Login" />
			</label>
		</form>
	</div>
</body>
</html>