<?php
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate');

$ip = isset($_GET['ip']) ? trim((string) $_GET['ip']) : '';
if ($ip === '::1' || $ip === '127.0.0.1') {
    $ip = '127.0.0.1';
}
$threshold = 15;
$online = false;

if ($ip !== '') {
    $file = __DIR__ . DIRECTORY_SEPARATOR . 'last_seen.json';
    if (is_file($file)) {
        $raw = @file_get_contents($file);
        if ($raw !== false) {
            $data = json_decode($raw, true);
            if (is_array($data) && isset($data[$ip])) {
                $last = (int) $data[$ip];
                $online = (time() - $last) <= $threshold;
            }
        }
    }
}

echo json_encode(['online' => $online, 'ip' => $ip]);
