 <?php

  if(isset($_POST['login'])){

    extract($_POST);

    $password = mysqli_real_escape_string($conn, $password);
    $user_name = mysqli_real_escape_string($conn, $user_name);

    $sql = "SELECT * FROM app_user WHERE user_name = '{$user_name}' LIMIT 1";
    $response = $conn->query($sql);
    if(mysqli_num_rows($response) > 0) {
      $row = $response->fetch_assoc();
      $stored_hash = $row['password'];
      $verified = password_verify($password, $stored_hash);
      // echo $verified;

      if ($verified) {
        
        $_SESSION['user_id'] = $row['id'];
        $_SESSION['user_name'] = $row['user_name'];
        $_SESSION['name'] = $row['name'];

        // time in
        $_SESSION['time_in_second'] = 180;
        $_SESSION['start_time'] = time();
        $_SESSION['time_in'] = time();
        header("Location: ../");
        exit();
      }else{
        echo "<script> alert('Username or Password is incorrect!'); </script>";
        // header("Location: https://google.com");
      }
    }else{
      echo "<script> alert('Username or Password is incorrect!'); </script>";
    }
  }
  ?>