开发者

How to detect internet speed in PHP?

开发者 https://www.devze.com 2023-02-21 13:19 出处:网络
How can I create a PHP page that will detect the user开发者_StackOverflow中文版\'s internet speed and show it on the page?Something like,

How can I create a PHP page that will detect the user开发者_StackOverflow中文版's internet speed and show it on the page? Something like,

Your internet speed is ??? Kbps


This might not be completely what you're looking for (read the bold part), but I doubt if anything else is possible.

This script sends 512 KB of HTML comments to your client. Parsing that HTML may add to the total transfer time, so don't take this as your raw download speed.

Quoted from: PHP Speed test

Source is here:

http://jan.moesen.nu/code/php/speedtest/index.php?source=1

Hope that helps.


<?php
$kb=1024;
echo "streaming $kb Kb...<!-";
flush();
$time = explode(" ",microtime());
$start = $time[0] + $time[1];
for($x=0;$x<$kb;$x++){
    echo str_pad('', 1024, '.');
    flush();
}
$time = explode(" ",microtime());
$finish = $time[0] + $time[1];
$deltat = $finish - $start;
echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
?>


For example by timing AJAX request on client side. That way you can figure approximate download speed, but not upload. For uploading, sending large AJAX POST request can handle it.

With jQuery and $.ajax it's pretty trivial to do.


By user uploading a file to your server. Then you divide file size in kb with time passed in seconds. You then get kb/s (upload speed).

$kb = round(filesize("file.jpg") / 1024); // 500kb
$time = time() - $start; // 5s
$speed = round($kb / $time); // 100kb/s


This works for me :

    $kb=512;
    echo "streaming $kb Kb...<!-";
    flush();
    $time = explode(" ",microtime());
    $start = $time[0] + $time[1];
    for($x=0;$x<$kb;$x++){
        echo str_pad('', 1024, '.');
        flush();
    }
    $time = explode(" ",microtime());
    $finish = $time[0] + $time[1];
    $deltat = $finish - $start;
    echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
    ?>

I got this from here.


Not really possible. PHP is server sided, detecting speed would be client sided.

You may find work arounds to do it, tho.

0

精彩评论

暂无评论...
验证码 换一张
取 消