<?php
       include 'includes/database.php';
       include 'includes/clientip.php';
$db=new MySQLDatabase();
$db->open_connection();
$timenow=date('Y-m-d H:i:s');
$ip=@get_client_ip();
session_start();
$logintype = 0;         // logintype 1 as from accesslevels 

		@$username = mysqli_real_escape_string($db->connection,  $_POST["username"] );
		@$password = mysqli_real_escape_string($db->connection,  $_POST["password"] );
 
		if( empty($username) || empty($password) )
			echo "Username and Password Mandatory ! - Authorisation Failed";
		else
		{
	    $sql = "SELECT count(*) FROM accesslevels WHERE( username='$username' AND password='$password')";
	    $res = mysqli_query($db->connection, $sql) or die(mysqli_error($db->connection));
        while ($row = mysqli_fetch_array($res, MYSQLI_NUM)) {
                $id=$row[0];
		if( intval($row[0]) > 0 ){
                 $query1=mysqli_query($db->connection, "INSERT into loginhistory (`username`,`password`,`logintime`,`loginip`) VALUES('$username','$password','$timenow','$ip')") or print "Database Error: ".mysqli_error($db->connection);
				 
		 $sql1 = "SELECT * FROM accesslevels  WHERE(`username`='$username' AND `password`='$password')";
		 $res2 = mysqli_query($db->connection, $sql1);               
			while ($row1 = mysqli_fetch_array($res2, MYSQLI_ASSOC)) {                      
                            $_SESSION['reusergroup'] = intval($row1['user_group']);
                            $_SESSION['reusername'] = $username; 
				   $logintype = 1;
			}			
         }
		else
		 echo "<font color=red>Wrong Username/Password</font>";
   		} 
 
        if( $logintype  > 0 ){ echo "<font color=white> Login Successful</font>"; 
                               echo '<script type=\'text/javascript\'>window.location=\'desktop.php\';</script>';
                                }
        else { echo "<font color=white> Login Unsuccessful !</font>"; }                
        }		
 
?>

