<?php
session_start();
require_once "config/db.php";

// ================= REMOVE REMEMBER TOKEN =================
if(isset($_SESSION['user_id'])){
    $stmt = $conn->prepare("UPDATE users SET remember_token=NULL WHERE id=?");
    $stmt->bind_param("i", $_SESSION['user_id']);
    $stmt->execute();
}

// ================= DESTROY SESSION =================
$_SESSION = [];
session_unset();
session_destroy();

// ================= DELETE COOKIE =================
if(isset($_COOKIE['remember_token'])){
    setcookie("remember_token", "", time() - 3600, "/");
}

// ================= REDIRECT =================
header("Location: login.php");
exit;
