Sharon Haim Pour
Sharon Haim Pour

Reputation: 6713

How to detect internet speed in PHP?

How can I create a PHP page that will detect the user's internet speed and show it on the page? Something like,

Your internet speed is ??? Kbps

Upvotes: 18

Views: 46428

Answers (6)

Martin
Martin

Reputation: 111

<?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";
?>

Upvotes: 11

Joris Ooms
Joris Ooms

Reputation: 12038

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.

Upvotes: 11

Anand Singh
Anand Singh

Reputation: 2363

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.

Upvotes: 1

Olli
Olli

Reputation: 1245

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.

Upvotes: 6

bl00dshooter
bl00dshooter

Reputation: 991

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

You may find work arounds to do it, tho.

Upvotes: -3

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

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

Upvotes: 5

Related Questions