<?php
session_start(); // Start the session to store the user ID

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Get the form data
  $email = $_POST['email'];
  $password = $_POST['password'];

  // Validate the form data
  if (empty($email) || empty($password)) {
		$error = "Please enter your email and password.";
  } else {
    // Check the user's credentials against the database
    $conn = mysqli_connect('localhost', 'kvfxminr_token', 'qHj}Sa7k(_4f', 'kvfxminr_token');
    $sql = "SELECT id FROM users WHERE email = '$email' AND password = '$password'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 1) {
      // Store the user ID in a session variable and redirect to the dashboard
      $row = mysqli_fetch_assoc($result);
      $_SESSION['user_id'] = $row['id'];
      // header("Location: welcome/index.php?id=" . $row["id"]);

      $user = mysqli_query($conn, "SELECT * FROM users WHERE email = '$email' AND password = '$password'");
  		if ($user && mysqli_num_rows($user) > 0) {
  		$row = mysqli_fetch_assoc($user);
  		$_SESSION['user_id'] = $row['id'];
  		if ($row['user_confirmed'] == 1) {
  		    // Allow the user to access their account
  		    // Redirect to the original index page
  		    header("Location: welcome/index.php?id=" . $row["id"]);
  		    exit();
  		} else {
  		    // Redirect the user to a page asking them to confirm their account
  		    header("Location: welcome/confirm.php?id=" . $row["id"]);
  		    exit();
  		}
  	}
      exit();
    } else {
			$error = "Incorrect email or password.";
    }

    mysqli_close($conn);
  }
}
?>

<!DOCTYPE html>
<html lang="zxx" class="js">
<head>
	<meta charset="utf-8">
<meta name="author" content="Softnio">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<!-- Fav Icon  -->
<link rel="shortcut icon" href="images/favicon.png">
<!-- Site Title  -->
<title>Login | ICO TOKENS FX</title>
<!-- Bundle and Base CSS -->
<link rel="stylesheet" href="assets/css/vendor.bundle.css?ver=1930">
<link rel="stylesheet" href="assets/css/style.css?ver=1930" id="changeTheme">
<!-- Extra CSS -->
<link rel="stylesheet" href="assets/css/theme.css?ver=1930">




</head>



    <body class="nk-body body-wider bg-light-alt">

	<div class="nk-wrap">

        <main class="nk-pages nk-pages-centered bg-theme">
            <div class="ath-container">
                <div class="ath-header text-center">
                    <a href="#" class="ath-logo"><img src="images/logoo.jpg" srcset="images/logoo.jpg 2x" alt="logo"></a>
                </div>
                <div class="ath-body">
                    <h5 class="ath-heading title">Login <small class="tc-default">with your ICO Account</small></h5>
										<?php if (isset($error)) : ?>
                     <p style="color: red;"><?php echo $error; ?></p>
                    <?php endif; ?>
                    <form method="post" action="page-login.php">
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="email" class="input-bordered" name="email" placeholder="Your Email" required>
                            </div>
                        </div>
                        <div class="field-item">
                            <div class="field-wrap">
                                <input type="password" class="input-bordered" name="password" placeholder="Password" required>
                            </div>
                        </div>
                        <div class="d-flex justify-content-between align-items-center pdb-r">
                            <div class="field-item pb-0">
                                <!-- <input class="input-checkbox" id="remember-me-2" type="checkbox">
                                <label for="remember-me-2">Remember Me</label> -->
                            </div>
                            <div class="forget-link fz-6">
                                <a href="#">Forgot password?</a>
                            </div>
                        </div>
                        <button class="btn btn-primary btn-block btn-md">Login</button>
                    </form>
                    <!-- <div class="sap-text"><span>Or Sign In With</span></div> -->

                    <!-- <ul class="row gutter-20px gutter-vr-20px">
                        <li class="col"><a href="#" class="btn btn-md btn-facebook btn-block"><em class="icon fab fa-facebook-f"></em><span>Facebook</span></a></li>
                        <li class="col"><a href="#" class="btn btn-md btn-google btn-block"><em class="icon fab fa-google"></em><span>Google</span></a></li>
                    </ul> -->
                </div>
                <div class="ath-note text-center tc-light">
                    Don’t have an account? <a href="page-register.php"> <strong>Sign up here</strong></a>
                </div>
            </div>
        </main>

				<footer class="nk-footer bg-theme-dark section-connect">
 					 <div class="section section-m pb-0 tc-light ov-h">

 							 <div class="nk-ovm shape-contain shape-center-top shape-p"></div>
 					 </div>
 					 <div class="section section-footer section-s tc-light bg-transparent">
 							 <div class="container">
 									 <!-- Block @s -->
 									 <div class="nk-block block-footer">
 											 <div class="row">
 													 <div class="col">
 															 <div class="wgs wgs-text text-center mb-3">
 																	 <div class="copyright-text copyright-text-s3 pdt-m">
 																			 <p><span class="d-sm-block">Copyright &copy; 2023, ICO Crypto. All Rights Resevered.</p>
 																	 </div>
 															 </div>
 													 </div><!-- .col -->
 											 </div><!-- .row -->
 									 </div><!-- .block @e -->
 							 </div>
 					 </div>
 			 </footer>

	</div>
	<div class="preloader"><span class="spinner spinner-round"></span></div>

	<!-- JavaScript -->
	<script src="assets/js/jquery.bundle.js?ver=1930"></script>
	<script src="assets/js/scripts.js?ver=1930"></script>
	<script src="assets/js/charts.js"></script>
</body>
</html>
