Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

usamamashkoor's avatar

determine user internet conection speed using javascript or php

Hi i want to determine user internet speed using javscript or php to display different videos resolution according to the user internet connection using video.js here is what i have got till now in php

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

but this is not working correctly it is giving me different speed each time..

Actually i want to display different video resolution on user speed...

and i want to get the client user internet speed in kbs..

kindly help me on this issue how i can i do this.

Thanks

0 likes
7 replies
ohffs's avatar

It'll be surprisingly fiddly to get an accurate number. I think your best bet is to use JS and make a plain http get for a file of known size and time that and disable caching. Off the top of my head, something like :

img.src = 'http://yourdomain.com/test.png?' + Math.random();
start = Date.now();
return $(img).load(function ()
{
  end = Date.now();
  // whatever
});

Not entirely sure that'll work - but there go. I think jquery's ajax calls have an option to disable caching too.

You still won't get a very consistent or reliable number though - if someone is downloading something else at the time for instance, then your number will be totally different.

usamamashkoor's avatar

@ohffs uploading file to server then getting the response back it will take time.but i am displaying the user the video when the page is loaded.. Can you please suggest me an alternative workout like youtube does with auto button...

Thanks

ohffs's avatar

You don't need to upload a file. Assuming you have some content on the player page (icons, banners, whatever) - just time it using (roughly) the method above and use that to set the default quality on the player. Set that as the default guess on the users session (and update that if the user over-rides it) and you can skip the timing check next time.

If you're loading & starting to play the video on page-load then I think you're onto a loser though - other than storing what the user picks in their session.

I generally find sites that auto-guess my bandwidth don't work and I need to set it manually anyway - so ymmv :-)

usamamashkoor's avatar

in your above example code you are just getting start and end time how does it help us to get the bandwidth speed of the user..

ohffs's avatar

Because you know the size of the file - so if you know how long it takes to download, you have a very rough ball-park idea of how fast their connection is.

usamamashkoor's avatar

@ohffs if the file size is 512 kb then can you please guide me how can i measure the connection speed for these

360p    1 Mbps
480      2 Mbps
720p    4 Mbps

it will be very great if you can update your script according to these

 img.src = 'http://yourdomain.com/test.png?' + Math.random();
start = Date.now();
return $(img).load(function ()
{
  end = Date.now();
  // whatever
});
ohffs's avatar

o_O

  • 1megabyte == 8megabits

  • Download a 1megabyte file in 1second == 8mbs connection

  • Download a 0.5megabyte file in 0.5seconds == 8mbs connection

  • Download a 0.5megabyte file in (0.5 * 2) seconds == (8 / 2) mbs connection

From there you can do the calculations for other mbs, hopefully.

Please or to participate in this conversation.