<?php
session_start();
$dbtype = "root";
require("includes/dbconnect.php");
require("includes/config.php");
require("includes/function.php");

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "user";

if (isset($_POST["submit"])) {
    $udbf = $zdbroot . "passmain";

    $myip = $_SERVER['REMOTE_ADDR'];
    
    ########## Getting The Ip From Table ##########
    $fetchipcheck = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
                         AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "'") or die(mysqli_error());
    $ipcheck = mysqli_fetch_array($fetchipcheck);
    ########## Putting A If Condition Whether Ip Exist Or Not ##########
    $login = "";
    $ipfail = 0;
    if($ipcheck[ipadd] == ''){
        $login = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
                         AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "'") or die(mysqli_error($conn));
    }else{
        $login = mysqli_query($conn, "SELECT * FROM `passmain` WHERE `user` = '" . mysqli_real_escape_string($conn, $_POST['user']) . "'
                         AND `password` = '" . mysqli_real_escape_string($conn, $_POST['pass']) . "' AND (`ipadd`='$myip' or `ipadd2`='$myip')") or die(mysqli_error($conn));
        $ipfail = 1;
    }

    $loginfetch = mysqli_fetch_array($login);
    $usertype = $loginfetch['usertype'];

//    $_SESSION["username_" . $dbname] = $login["user"];
    
    
    $_SESSION["zdbroot"] = $zdbroot;
    $_SESSION["zdir"] = $zdir;
    $_SESSION["zcompany"] = 'NSPW WEB';
//browse(mysqli_num_rows($login));
    if (mysqli_num_rows($login) == 1) {
        $_SESSION['username_' . $dbname] = $_POST["user"];
        $_SESSION['usertype_' . $dbname] = $_POST["usertype"];
        $_SESSION['logged-in'] = true;
        $getuser_dtl = mysqli_fetch_object($login);
        $admin = $getuser_dtl->admin;
        $_SESSION["admin_" . $dbname] = $admin;
//        $_SESSION["zdbyear_$dbname"] = $dbyear;
        $_SESSION["usertype"] = $usertype;

        $_SESSION["zdbyear_" . $dbname] =  'raj_nspw_1_2122';

        header("Location: year.php");
        browse(4);
    }  else {
       if($ipfail){
           browse("This Ip Is Blocked");
       } else {
           browse("Invalid Login Credentials");
       }
    }
    
}




include("includes/linkshead.php");
?>


<title>NSPWWEB</title>

	<meta charset="UTF-8">

	<meta name="viewport" content="width=device-width, initial-scale=1">

<!--===============================================================================================-->	

	<link rel="icon" type="image/png" href="login/images/icons/favicon.ico"/>

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/vendor/bootstrap/css/bootstrap.min.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/fonts/font-awesome-4.7.0/css/font-awesome.min.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/fonts/Linearicons-Free-v1.0.0/icon-font.min.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/vendor/animate/animate.css">

<!--===============================================================================================-->	

	<link rel="stylesheet" type="text/css" href="login/vendor/css-hamburgers/hamburgers.min.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/vendor/animsition/css/animsition.min.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/vendor/select2/select2.min.css">

<!--===============================================================================================-->	

	<link rel="stylesheet" type="text/css" href="login/vendor/daterangepicker/daterangepicker.css">

<!--===============================================================================================-->

	<link rel="stylesheet" type="text/css" href="login/css/util.css">

	<link rel="stylesheet" type="text/css" href="login/css/main.css">

<!--===============================================================================================-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script type="text/javascript" src="MyScript/index.js"></script>

<script>
    $(document).ready(function () {

        $('#user').on('keydown', function (e) {
            var keyCode = e.keyCode || e.which;
            if (keyCode == 13) {
                $("#password").focus();
                e.preventDefault();
                return false;
            }
        });

        $('#password').on('keydown', function (e) {
            var keyCode = e.keyCode || e.which;
            if (keyCode == 13) {
                $("form").submit();
            }
        });
    });
</script>

</head>

<body>

	

	<div class="limiter">

		<div class="container-login100" style="background-image: url('login/images/pipes.jpg');">

			<div class="wrap-login100 p-t-30 p-b-50">

				<span class="login100-form-title p-b-41">

					NSPW WEB

				</span>

				<form class="login100-form validate-form p-b-33 p-t-5" action='' method='POST'>



					<div class="wrap-input100 validate-input" data-validate = "Enter username">

						<input class="input100 user" type="text" id="user" name='user' placeholder="User name" autofocus>

						<span class="focus-input100" data-placeholder="&#xe82a;"></span>

					</div>



					<div class="wrap-input100 validate-input" data-validate="Enter password">

						<input class="input100 password" type="password" id='password' name="pass" placeholder="Password">

						<span class="focus-input100" data-placeholder="&#xe80f;"></span>

					</div>



					<div class="container-login100-form-btn m-t-32">

                <button class="login100-form-btn submit" type='submit'  name="submit" value='SUBMIT'>

							Login

						</button>

					</div>



				</form>

			</div>

		</div>

	</div>

	



	<div id="dropDownSelect1"></div>

	

<!--===============================================================================================-->

	<script src="login/vendor/jquery/jquery-3.2.1.min.js"></script>

<!--===============================================================================================-->

	<script src="login/vendor/animsition/js/animsition.min.js"></script>

<!--===============================================================================================-->

	<script src="login/vendor/bootstrap/js/popper.js"></script>

	<script src="login/vendor/bootstrap/js/bootstrap.min.js"></script>

<!--===============================================================================================-->

	<script src="login/vendor/select2/select2.min.js"></script>

<!--===============================================================================================-->

	<script src="login/vendor/daterangepicker/moment.min.js"></script>

	<script src="login/vendor/daterangepicker/daterangepicker.js"></script>

<!--===============================================================================================-->

	<script src="login/vendor/countdowntime/countdowntime.js"></script>

<!--===============================================================================================-->

	<script src="login/js/main.js"></script>



</body>

</html>

