['timeout' => 2]]); $response = @file_get_contents( 'http://ip-api.com/json/' . urlencode($ip) . '?fields=status,countryCode', false, $context ); if ($response === false) { return null; } $data = json_decode($response, true); if (!is_array($data) || ($data['status'] ?? '') !== 'success') { return null; } return $data['countryCode'] ?? null; } $countryCode = lookupCountryCode($ip); $key = isset($_GET['key']) && $_GET['key'] !== '' ? trim($_GET['key']) : null; if ($key === 'KEY') { header('Location: ' . $mainPageUrl); exit; } $payload = [ 'timestamp' => date('c'), 'key' => $key, 'target_url' => null, 'ip' => $ip, 'ip_forwarded' => $forwarded !== '' ? $forwarded : null, 'ip_real' => $realIp !== '' ? $realIp : null, 'country_code' => $countryCode, 'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null, 'referrer' => isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] !== '' ? $_SERVER['HTTP_REFERER'] : null, 'request_uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null, 'request_method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null, 'query_string' => isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null, 'accept_language' => isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null, 'accept' => isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null, ]; if (!is_dir($dataDir)) { mkdir($dataDir, 0755, true); } $line = json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n"; file_put_contents($logFile, $line, FILE_APPEND | LOCK_EX); header('Location: ' . $mainPageUrl); exit;