<?php
session_start();
if(isset($_SESSION['username'])&& isset($_SESSION['userType']) && isset($_SESSION['password']))
{
	if($_SESSION['userType']=='admin'){
		header('location:./admin');
		exit();
	}
}
?>
<?php

if(isset($_POST['username']) && isset($_POST['password']))
{
	$username =$_POST['username']; //Perform a regular expression search and replace
	$password =$_POST['password'];
	 include("includes/connection.php");	 
			$sql=mysql_query("select * from users where username='$username' and password='$password'");
			$count=mysql_num_rows($sql);
			$row=mysql_fetch_array($sql);			
			if($count==1 && $username==$row['username'] && $password==$row['password'])
			{
				session_regenerate_id(true);  //Update the current session id with a newly generated one
				$_SESSION['username']=$username;
				$_SESSION['password']=$password;
				$_SESSION['userType']=$row['type'];
				
				if($_SESSION['userType']=='admin'){
					header('location:./admin');
					exit();
				}
			}
			else
			{
				echo '<script type="text/javascript"> alert("Wrong Username or Password!");</script>';
			}
	}
?>