Determine connection speed with php

One of the hardest script you can find in internet is the one that determines connection speed of a surfer visiting our site.
From today it’s not anymore a problem (even if nobody had nightmares before…) because we will make a php script in order to fill such lack.

Let’s start coding…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$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";
?>

Done. Analysis and considerations:

2: Declaring how much Kb I want to transmit to accomplish the test. This value can also be passed in POST or GET. In my example it is declared.

3: I write I am downloading data, and open a comment tag. Everithing I am sending from now on will not be displayed. It’s not necessary, but prevents the browser to be fullfilled of chars.

4: Flush the chache

5-6: Saving actual timestamp

7-9: For $kb times I send 1024 chars (1Kb) and flush the cache

11-12: Saving actual timestamp

13: Calculating difference between start and finish timestamps

14: Closing comment tag and writing test result, calculated as Kb/time

Done… very easy isn’t it?

Considerations and exceptions

I wasn’t able to get reliable speed values with $kb under 512. I noticed the higher this number, the more reliable the result.

In some php configurations, those with output buffering to “On”, php executes before sending headers, cookies, and so on, and the time calculated in that way is the time of pure execution (excluding tranfers). Disabling output buffering, however, results are reliable.

You can try the script at this page.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 4.14 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 26 comments

  1. jacob

    on June 9, 2006 at 8:23 pm

    it doesn’t reflect true values

  2. isoveli

    on June 23, 2006 at 10:36 am

    this script is greatly affected by the server this script is in; http://www.triquitips.com/timetest.php claims my speed is ~231 Kb/s but the script on my own server says it is nearly 395 Kb/s

  3. colin

    on July 19, 2006 at 12:29 pm

    Provides an excellent and very useful way of giving visitors an approximate duration for subsequent downloads. Thank you.

  4. Michel Bozgounov

    on October 13, 2006 at 2:44 pm

    Interesting! :-)

  5. Wha...??!!?

    on December 17, 2006 at 4:25 pm

    uhhh… I have a 8.1Mb/sec connection, and it says that i have a speed of 303Kb/sec! I am now gonna test with a speed tester I downloaded…… (http://myspeed.com/)… That says 6.1 Mb/sec (that’s what can be expected, if you are british, you will know that BT is :(:(:(:(:(:( )

    But the upload speed is 376Kb/sec… any way this might link?

  6. Liam

    on May 17, 2007 at 6:20 pm

    a useful script!! ideal for my entertainment site to tell people how long it will take until the flash runs!! good job.

  7. Cody Ray

    on July 23, 2007 at 10:29 pm

    This does not reflect the actual connection speed. What this does tell you is how long the CPU on the server takes to execute this script, as all processing is done server-side in PHP. Try again.

  8. Tippi

    on August 31, 2007 at 2:26 pm

    Just one fault:

    If the server’s bandwidth is lower then the clients the servers bandwidth will be reported as the speed…

    This can be fixed, i have rewritten you code with some of mine, basiclly you should set the timestamps just before and after the echo.
    Becuse 1024 bytes for sure can be delivered at max speed with the any connection today, 512kb can’t :)

    Then when that loop is done, sum all up and make a avarage and do ur calculation.

    Send me a mail if you want the source for the modified version. Cheers.

  9. RR

    on October 21, 2007 at 3:48 pm

    Tippi: can you please post the code?

  10. deo

    on December 7, 2007 at 4:42 am

    how about to determine my connection to a web server?? Tippi post your code, please.

  11. Determine connection speed

    on April 6, 2008 at 12:49 am

    determine connection is easy to find with above written code

  12. getpr

    on April 16, 2008 at 9:39 am

    This can be fixed, i have rewritten you code with some of mine, basiclly you should set the timestamps just before and after the echo.
    Becuse 1024 bytes for sure can be delivered at max speed with the any connection today, 512kb can’t :)

  13. saki

    on July 10, 2008 at 11:22 am

    tippi plz post the code na.. which gives internet speed of the client usiing php script.. this script didnt work.. when kept in the server.. :(

  14. Robert

    on November 13, 2008 at 4:41 pm

    Can tipi please email that code; or can you post it here?

    or can someone who has it send it me?
    rgray001@googlemail.com
    regards

  15. Paul Romein

    on December 17, 2008 at 11:30 am

    It looks like what it’s grabbing is the user’s upload speed. This can be helpful to get a guestimate on the pipe, but on a quick Google search for a tool like it, it’s not bad. Just needs to explain that it’s the users’ upload speed, not the user’s download speed.

  16. Ezekiel

    on April 7, 2009 at 3:18 pm

    This script is very useful, I am going to test it. Definitely the location of the server will affect the results. My recommendation is that if users want to test the speed of their links (as given by ISPs) then they should test with a website resident at the ISP. This means you will be testing only the path from you to the ISP, excluding all the unnecessary hops. I will further develop it, nice starting point.

  17. mark_valley

    on June 19, 2009 at 7:23 pm

    Nice script! Of course the result depends on the server’s location.

    I adjusted it a bit for my needs. Instead of echoing each character, i build a randon string whith size $kb and echo it. Something like that:

    ————————————————-

    for($x = 0; $x < ($kb * 1024); $x++)
    {
    $data .= chr(rand(97, 97+20));
    }
    flush();

    $time = explode(" ",microtime());
    $start = $time[0] + $time[1];

    echo "”;

    $time = explode(” “,microtime());
    $finish = $time[0] + $time[1];

    —————————————————-

  18. Dennis Dykstra

    on December 7, 2009 at 6:18 pm

    This seems like a useful approach. I’ve been trying to distinguish between dialup and broadband users in order to avoid sending a lot of images to a slow dialup line. However, when I use the PHP script on a dialup line (actual connection speeds between 45.2 and 50.6 Kbps), I get an odd result: the reported connection speed with your script ranges between 20 and 140 Kbps, with an average of nearly 100 Kbps. Why do you think it’s so high? What is it measuring, if not the actual time to send the 512 KB to the browser?

  19. Vinod

    on April 6, 2010 at 5:48 pm

    Chkd it , simple , useful… love it , thank you

  20. Gaelos

    on August 4, 2010 at 11:49 pm

    is it possible to show the bandwidth in real time during the test ?

  21. Sergey Lempert

    on November 26, 2010 at 4:24 pm

    Great script. I am not sure it will work on dial-up, but it’s not so popular in the last years.

  22. Todd

    on April 29, 2011 at 7:59 pm

    I think the problem is that flush() does not “wait” for the output to reach the client, so you really can’t tell how long the data is taking to get there. In fact, depending on the web server, flush() may not even send the output buffer (it still gets buffered).

  23. John

    on May 13, 2011 at 7:15 pm

    Emanuele Feronato What you mean by connection speed???
    The above script never find the the connection speed.
    It jus says the php script exicuation speed.
    It totaly depan upon the server. What connection speed you find here?

    The result of the above code is the Script exicuation speed. There is calcation to find the connection speed in the given script. You please change the heading of this post to avoid miss leading people.

  24. John

    on May 13, 2011 at 7:45 pm

    How did you calculate my Internet connection speed?

    Answer: Your connection speed was calculated on how fast your computer received the first file.

    Formula
    Amount of Data/Time to Download = Connection Speed

    If you have 1Kb, it is the time taken by your borwser to recive 1kb.

    Emanuele Feronato’s PHP script find out total time to exicuate the script.

  25. fireduck

    on December 12, 2011 at 2:17 pm

    Thanks for this source code…

    Since we are not using old fashion (56k )dial up connection, we have a broadband connection from 5mb to 100mb…

    this code will convert from Kbps to Mbps

    you are sending data as: 1mb which is equivalent to 1024kb (1*1024 = 1024kb)

    $kb=1024; //(change anything from 1-10mb – rem to convert into kb)

    echo “streaming $kb Kb…<!-";
    flush();
    $time = explode(" ",microtime());
    $start = $time[0] + $time[1];
    for($x=0;$x Test finished in $deltat seconds. Your speed is “. $MbpsResult.”Mbps”;

    have fun!!

  26. fireduck

    on December 12, 2011 at 2:17 pm

    $kb=1024;

    echo “streaming $kb Kb…<!-";
    flush();
    $time = explode(" ",microtime());
    $start = $time[0] + $time[1];
    for($x=0;$x Test finished in $deltat seconds. Your speed is “. $MbpsResult.”Mbps”;