
<?php
include "config.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  //  header('Content-Type: text/html; charset=UTF-8');
    $login = htmlspecialchars($_POST['login']);
    $password = htmlspecialchars($_POST['password']);

    // Parametrizēts SQL pieprasījums
    $sql = $conn->prepare("SELECT * FROM users WHERE username = ?");
    $sql->bind_param("s", $login);
    $sql->execute();
    $result = $sql->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();

        // Pārbaudiet, vai ir 'password_hash' un salīdziniet
        if (isset($row['password']) && password_verify($password, $row['password'])) {
            session_start();
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];
            echo "1";//"Login successful";
        } else {
            echo "0";//"Incorrect password";
        }
    } else {
        echo "Incorrect username";
    }

    $conn->close();
}
?>
