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.
18 Responses to “Determine connection speed with php”
Leave a Reply
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)


(7 votes, average: 3.86 out of 5)





it doesn’t reflect true values
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
Provides an excellent and very useful way of giving visitors an approximate duration for subsequent downloads. Thank you.
Interesting! :-)
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?
a useful script!! ideal for my entertainment site to tell people how long it will take until the flash runs!! good job.
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.
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.
Tippi: can you please post the code?
how about to determine my connection to a web server?? Tippi post your code, please.
determine connection is easy to find with above written code
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 :)
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.. :(
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
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.
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.
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];
—————————————————-
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?