<?
session_start();
$brcode=$_SESSION['brcode'];
if(strlen($brcode)>0)
{
 //print "<script> alert('You are already logged in as $user'); </script>";
 //print "<center>[<a href='logout.php'>Logout</a>] ";
 //print "<br><font color=red><blink><b>Warning </b></font></blink><br>Logout properly in future <br> Multiple login is not allowed";
 //return;
}
$uname=$_REQUEST['uname'];
$pswd=$_REQUEST['pswd'];
$br=$_REQUEST['br'];
include("opendb.php");
$database1='psnindia_gen4psnauto_psn_psndb2324';
$result=mysqli_query($db,"select * from branch where brname='$br'")or die(mysqli_error());
$rr=mysqli_fetch_row($result);
$br_name=$rr[0];


if($uname=='admin')
{
    $result=mysqli_query($db,"select * from branch where brname='$br' and brpass='$pswd'")or die(mysqli_error());
    $rr=mysqli_fetch_row($result);
    if(mysqli_num_rows($result)>0)
    {
	 session_register("user");
	 $user=strtoupper($uname);
	 session_register("branch");
	 $branch=$rr[1];
	 session_register("brcode");
	 $brcode=$rr[0];
	 echo "<meta http-equiv='refresh' content='.1;url=indexbr.htm'>";
    }
    else
        print "<script> alert('Invaid admin Login'); history.back(1); </script>";
    }
else
{
    $sql="select * from bruser where username='$uname' and pswd='$pswd' and branch='$br_name' and utype='active'";
    $result=mysqli_query($db,$sql)or die(mysqli_error());
    $user_row=mysqli_fetch_array($result);
    $re=mysqli_query($db,"select * from branch where brname='$br'")or die(mysqli_error());
    $rr=mysqli_fetch_row($re);
    $dt=$user_row['passdate'];
    //echo $dt;
    $sq="select to_days(curdate())-to_days('$dt')";
    $dtre=mysqli_query($db,$sq)or die(mysqli_error());
    $dtrow=mysqli_fetch_row($dtre);
    $xy=0;
    $xy= 90-$dtrow[0];
    $yr='2010-2011';
    //print $xy;

if($dtrow[0]>90)
{
 print "<script> alert('Password expired'); history.back(1); </script>";
 return;
}
if(mysqli_num_rows($result)>0)
{
    /*print "aaaaaaaaaa";
	 session_register("user");
	 $user=$uname;
	 print "aaaaaaaaaa";
	 session_register("branch");
	 $branch=$br;
	 session_register("brcode");
	 $brcode=$rr[0];
	 session_register("level");
	 $level=$user_row[level];
	session_register("xyy");
	 $xyy=$xy;
	session_register("yr");
	 $yr=$yr;
	session_register("cccode");
	$cccode=$rr[7];*/
	
    $_SESSION["user"];
    $user=str_replace('.','',$uname);
    $_SESSION['user']=$uname;
    $_SESSION["branch"]=$rr[1];

    $_SESSION["brcode"];
    $brcode=$rr[0];
    $_SESSION["brcode"]=$brcode;
    $_SESSION["fin_year"];
    $_SESSION["fin_year"]=$fin_yr;
    //echo $fin_yr;
    //echo $brcode;
    $_SESSION["level"];
    $level=$user_row['level'];
    $_SESSION["level"]=$level;
    $_SESSION["cccode"];
    $cccode=$brtyp;
    $_SESSION['expir']=$xy;
    $_SESSION['xyy']=$xy;
    $_SESSION['yr']=$yr;
    include('Mobile_Detect.php');
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
date_default_timezone_set('Asia/Kolkata');
$dt=date("Y-m-d H:i:s");
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
    $ip = $_SERVER['REMOTE_ADDR'];
}


$qry1="select ipval from ipdtls where brcode='$br_name'";
$rs1=mysqli_query($db,$qry1);
$num=mysqli_num_rows($rs1); 
if($num>0)
{
    
    $qry="select ipval from ipdtls where brcode='$br_name' and ipval='$ip'";
    $rs=mysqli_query($db,$qry);
    $num1=mysqli_num_rows($rs);
    if($num1<=0)
    {   
        print "<script> alert('Invalid IP. Unable to login'); history.back(1);</script>";
        //$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
        //$ip_location= $details->city.", ".$details->region.", ".$details->country;
        $sql="insert into $database1.log_dup values('$br_name','$uname','$ip','$dt','$deviceType','$ip_location','Failed attempt')";
        mysqli_query($db,$sql);
        session_start();
        session_unset();
        return;
    }
}





//$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
//$ip_location= $details->city.", ".$details->region.", ".$details->country;
//$sqlnn="insert into $database1.log_dup values('$br_name','$uname','$ip','$dt','$deviceType','$ip_location','PSN Login')";
//mysqli_query($db,$sqlnn);
   
    
    
    print "Loading .......";
    echo "<meta http-equiv='refresh' content='.1;url=index1.php'>";
}
else
{
   print "<script> alert('Invalid Login'); history.back(1); </script>";
   return;
 } 
}
?>
