PHP Speed Test for user connection speed without echo in current page

I am looking for a possibility to check the user connection speed. It is supposed to be saved as a cookie and javascript files as well as css files will be adapted if the speed is slow.

The possibility for testing speed i have at the moment ist the following

    $kb = 512;

    flush();
    //
    echo "<!-";
    $time = explode(" ",microtime());
    for($x=0;$x<$kb;$x++){
        echo str_pad('', 512, '.');
        flush();
    }
    $time_end = explode(" ",microtime());
    echo "->";

    $start = $time[0] + $time[1];
    $finish = $time_end[0] + $time_end[1];
    $deltat = $finish - $start;

    return round($kb / $deltat, 3);

While it works, I do not like it to put so many characters into my code also if I echo all this I can not save the result in a cookie because there has already been an output.

Could one do something like this in a different file wor something? Do you have any solution?

Thanks in advance.


Solution 1:

Do you have any solution?

My solution is to not bother with the speed test at all. Here's why:

You stated that the reason for the test is to determine which JS/CSS files to send. You have to keep in mind that browsers will cache these files after the first download (so long as they haven't been modified). So in effect, you are sending 256K of test data to determine if you should send, say, an additional 512K?

Just send the data and it will be cached. Unless you have MBs of JS/CSS (in which case you need a site redesign, not a speed test) the download time will be doable. Speed tests should be reserved for things such as streaming video and the like.

Solution 2:

The only idea what i can come up is a redirect.

  • Measure users' speed
  • Redirect to index

While this isn't a nice solution it only need to measure users' speed only once so i think it's excusable.