<?php
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$uname=$_POST['username'];
$pwd=$_POST['password'];

require_once "config1.php";
$sql="select * from admin_login where uname = '".$uname."' and pwd='".$pwd."'";
$query=mysqli_query($con,$sql);

// $query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}

   
    
$data=array();
if($result=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
    if($result['pwd']!=$pwd){
        die("Invalid User");
    }
//   $dbotp = $result['otp'];
   
// 	if($otp != $dbotp)
// 	{
// 	  die("Invalid OTP!");
// 	}
   
   $ip= get_client_ip();
   date_default_timezone_set("Asia/Kolkata");
	$date= date("Y-m-d H:i:s");
    $sql1="insert into login_history(created_on,ip,device_name)values('".$date."','".$ip."','".$_SERVER['HTTP_USER_AGENT']."')";
    $query1=mysqli_query($con,$sql1);
    if($query1==false)
	{
        die(mysqli_error($con));
    }
   // session_start();
   
    ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 3 hour
session_set_cookie_params(3600);

session_start(); 
 $_SESSION['name']=$uname;
    echo 1; 
}else{
    echo 0;
}
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
?>