<?php 
ob_start();
session_start();
include_once("db_connect.php");
if(isset($_SESSION["logged_in"]) && $_SESSION["logged_in"]==true){
	echo '<div class="alert alert-warning" role="alert">You have already loggedin <a href="logout.php">Click here to logout</a></div>';
	exit;
}
$errormsg="";
if (isset($_POST['login'])) {
	$username = mysqli_real_escape_string($conn, $_POST['username']);
	$password = mysqli_real_escape_string($conn, $_POST['password']);
	if(empty($username) || empty($password)){
	   $errormsg .= '<div class="alert alert-danger" role="alert">Username & password required.</div>'; 
	} else {
	    $result = mysqli_query($conn, "SELECT * FROM users WHERE user = '" . $username. "' and pass = '" . md5($password). "'");
    	if(mysqli_num_rows($result) > 0){
    	    $row = mysqli_fetch_array($result);
    	    $_SESSION['user_id'] = $row['uid'];
    		$_SESSION['user_name'] = $row['user'];
    		$_SESSION['logged_in'] = true;
    		header("Location: indexnew.php");
    	} else {
    	    $errormsg .= '<div class="alert alert-danger" role="alert">Invalid username or password.</div>';
    	}
	}
}
include('header.php');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Online Registration and Student Login</title>
<link href="head_foot.css" rel="stylesheet" />
<script type="text/javascript" src="script/ajax.js"></script>
</style>
</head>

<header>
    <h2>R.S.P. College, Jharia(Belagaria), Dhanbad</h2>
  <h5>( A Constituent Unit of Binod Bihari Mahto koyalanchal University, Dhanbad )</h5>
</header>

<div class="container">
	<div class="row">
		<div class="col-md-4 col-md-offset-4 well">
		    <?php echo $errormsg; ?>
			<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform">
				<fieldset>
					<legend>Login</legend>						
					<div class="form-group">
						<label for="name">Username</label>
						<input type="text" name="username" placeholder="Your Username" required class="form-control" />
					</div>	
					<div class="form-group">
						<label for="name">Password</label>
						<input type="password" name="password" placeholder="Your Password" required class="form-control" />
					</div>	
					<div class="form-group">
						<input type="submit" name="login" value="Login" class="btn btn-primary" />
					</div>
					
				

				</fieldset>
			</form>
		</div>
	</div>
	<div class="row">
		<div class="col-md-4 col-md-offset-4 text-center">	
		New User? <a href="register.php">Sign Up Here</a>
		</div>
	</div>
		
</div>


<footer>
 <p></p>
 <p></p>
  <p></p>
   <p></p>
</footer>


<?php include('footer.php');?>