<?php
header('X-Frame-Options: ALLOWALL'); // OR SAMEORIGIN if you want only your domain to embed it

// Allow cross-origin requests (CORS)
header('Access-Control-Allow-Origin: *'); // Allow any domain to fetch the content
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

// Set content type
header('Content-Type: text/html');

// Function to get the real user IP
function getUserIP() {
    $keys = ['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR'];
    foreach ($keys as $key) {
        if (!empty($_SERVER[$key])) {
            $ip_list = explode(',', $_SERVER[$key]);
            return trim($ip_list[0]); // First valid IP
        }
    }
    return 'Unknown';
}

// Get user's IP
$user_ip = getUserIP();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loader</title>
</head>
<body>

    <div id="location"></div>

    <script>
        var userIP = "<?php echo $user_ip; ?>"; // Get IP from PHP

        // Fetch location info using ipapi.co
        fetch(`https://ipapi.co/${userIP}/json/`)
        .then(response => response.json())
        .then(jsonData => {
            // console.log(jsonData); // Log location data
            
            // Display location
            document.getElementById("location").textContent = 
                `${jsonData.country_name} ${jsonData.city}`;
        })
        .catch(error => console.log("Error fetching location: ", error));
    </script>

</body>
</html>
