<!DOCTYPE html>
<html>
<head>
    <title>Internet Speed Test</title>
</head>
<body>
    <h1>Internet Speed Test</h1>
    <button onclick="runSpeedTest()"><span>Run Speed Test</span></button>
    <div id="result"></div>

    <script>
        function runSpeedTest() {
            var startTime = new Date().getTime();
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '<?php echo $_SERVER['PHP_SELF']; ?>?action=speedtest');
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var endTime = new Date().getTime();
                    var fileSize = parseInt(xhr.getResponseHeader('Content-Length'));
                    var downloadTime = (endTime - startTime) / 1000; // in seconds
                    var downloadSpeed = (fileSize / downloadTime / 1024).toFixed(2); // in KB/s
                    document.getElementById('result').innerHTML = 'Download Speed: ' + downloadSpeed + ' KB/s';
                }
            };
            xhr.send();
        }
    </script>

    <?php
    if ($_GET['action'] == 'speedtest') {
        $fileUrl = 'https://videos.pexels.com/video-files/5532774/5532774-uhd_4096_2160_25fps.mp4?action=speedtest'; // URL to a sample file to download for the test
        $fileSize = filesize($fileUrl);
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="samplefile.bin"');
        header('Content-Length: ' . $fileSize);
        readfile($fileUrl);
        exit;
    }
    ?>
</body>
</html>
