By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 1,964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

detect internet speed with php

P: n/a
Hi guys,
Is it possible to know the internet speed of the visitors with php?

Thanx
--
Yang
Jul 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Yang Li Ke wrote:
Hi guys,
Is it possible to know the internet speed of the visitors with php?

No. There are too many things that have to happen to try to determine
the speed of the visitor. And most of those things, PHP has no access to.

--
Randy

Jul 17 '05 #2

P: n/a
Something like this might given you a sense of how fast the connection is:

function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}

function test_speed($test_size) {
flush();
$start_time = microtime();
$comment = "<!--O-->";
$len = strlen($comment);
for($i = 0; $i < $test_size; $i += $len) {
echo $comment;
}
flush();
$duration = microtime_diff($start_time, microtime());
if($duration != 0) {
return $test_size / $duration / 1024;
}
else {
return log(0);
}
}

$speed = test_speed(1024);
if($speed > 50) { // a fast connection, send more byte for more accuracy
$speed = test_speed(10240);
if($speed > 500) { // a really fast connection, send even more byte for
more accuracy
$speed = test_speed(102400);
}
}
echo sprintf("Download speed is %0.3f kb/s", $speed);

I have the code set up here: http://www.conradish.net/speed.php. On my cable
connection I get around 300 kb/s, which is about right.

Uzytkownik "Yang Li Ke" <ya******@sympatico.ca> napisal w wiadomosci
news:v7******************@news20.bellglobal.com...
Hi guys,
Is it possible to know the internet speed of the visitors with php?

Thanx
--
Yang

Jul 17 '05 #3

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:_f********************@comcast.com...
Something like this might given you a sense of how fast the connection is:

function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}

function test_speed($test_size) {
flush();
$start_time = microtime();
$comment = "<!--O-->";
$len = strlen($comment);
for($i = 0; $i < $test_size; $i += $len) {
echo $comment;
}
flush();
$duration = microtime_diff($start_time, microtime());
if($duration != 0) {
return $test_size / $duration / 1024;
}
else {
return log(0);
}
}

$speed = test_speed(1024);
if($speed > 50) { // a fast connection, send more byte for more accuracy
$speed = test_speed(10240);
if($speed > 500) { // a really fast connection, send even more byte for
more accuracy
$speed = test_speed(102400);
}
}
echo sprintf("Download speed is %0.3f kb/s", $speed);

I have the code set up here: http://www.conradish.net/speed.php. On my cable connection I get around 300 kb/s, which is about right.

Uzytkownik "Yang Li Ke" <ya******@sympatico.ca> napisal w wiadomosci
news:v7******************@news20.bellglobal.com...
Hi guys,
Is it possible to know the internet speed of the visitors with php?

Thanx
--
Yang



Interesting concept,

when I test your link, I average 240, but I am on 3 Mbit

when I run it localy with my test server, GigaBit from my desktop to 1
switch to server, I get 190

hmm, I think it is rather testing execution speed.
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #4

P: n/a
I wonder. According to the PHP manual, flush() is supposed to flush the
server's buffer as well. Presumably the function wouldn't return until it
has received an acknowledgement for the last packet sent.

I tried adding ob_start("ob_gzhandler") to the beginning of the file. The
measured speed increased nearly ten-fold. So I don't think it's testing the
execution speed.

Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:Uz******************@newssvr25.news.prodigy.c om...
Interesting concept,

when I test your link, I average 240, but I am on 3 Mbit

when I run it localy with my test server, GigaBit from my desktop to 1
switch to server, I get 190

hmm, I think it is rather testing execution speed.
--
Mike Bradley
http://www.gzentools.com -- free online php tools

Jul 17 '05 #5

P: n/a
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:NJ********************@comcast.com...
I wonder. According to the PHP manual, flush() is supposed to flush the
server's buffer as well. Presumably the function wouldn't return until it
has received an acknowledgement for the last packet sent.

I tried adding ob_start("ob_gzhandler") to the beginning of the file. The
measured speed increased nearly ten-fold. So I don't think it's testing the execution speed.

Hmm, here is an idea, havent done any code, I am going to sleep, diving in
the morning.

what if you were to send a header with a timestamp:

fist call to page:
Location:
http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime

next call to page
Location:
http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m
icrotime

loop a couple times of times, average out the time stamps for an average
page redirct.

this will be used a reference to delays, overhead etc...

then do this header
Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp

then for that page, send a lot of header data:
for ($i=0; $i < 100; $i++)
{
$head = "X-SpeedTest: --- speed test $i---";

$headerSize += strlen($head) + 2;
header($head);
}

header("Location:
http://www.blablabla.com/test.php?ac...=mictotime&ds=
$headerSize
then the last call to the page:

$timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
$dataSize = $_GET['ds'];

hmm, then I think we would have a size of data sent, and time to send?

just a thought.....What do you think?

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #6

P: n/a
Ok, I just got back, and tried to code it, let me know if this is any good.

you can test it here:

http://www.gzentools.com/test/speed.php

and the source is here:

http://www-3.gzentools.com/snippetvi...=speedtest.php

--
Mike Bradley
http://www.gzentools.com -- free online php tools

"CountScubula" <me@scantek.hotmail.com> wrote in message
news:Zx******************@newssvr25.news.prodigy.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:NJ********************@comcast.com...
I wonder. According to the PHP manual, flush() is supposed to flush the
server's buffer as well. Presumably the function wouldn't return until it has received an acknowledgement for the last packet sent.

I tried adding ob_start("ob_gzhandler") to the beginning of the file. The measured speed increased nearly ten-fold. So I don't think it's testing the
execution speed.

Hmm, here is an idea, havent done any code, I am going to sleep, diving in
the morning.

what if you were to send a header with a timestamp:

fist call to page:
Location:
http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime

next call to page
Location:

http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m icrotime

loop a couple times of times, average out the time stamps for an average
page redirct.

this will be used a reference to delays, overhead etc...

then do this header
Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp

then for that page, send a lot of header data:
for ($i=0; $i < 100; $i++)
{
$head = "X-SpeedTest: --- speed test $i---";

$headerSize += strlen($head) + 2;
header($head);
}

header("Location:
http://www.blablabla.com/test.php?ac...=mictotime&ds= $headerSize
then the last call to the page:

$timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
$dataSize = $_GET['ds'];

hmm, then I think we would have a size of data sent, and time to send?

just a thought.....What do you think?

--
Mike Bradley
http://www.gzentools.com -- free online php tools

Jul 17 '05 #7

P: n/a
Hmmm, using the header might be a better idea than sending the test bytes as
HTML comment, as it seems the browser would save the comment tags into the
DOM (thus eat up memory). A bit cleaner too to redirect to a separate page.

Tried your page. It's reporting that my 3 mbit connection is capable of
downloading 1.8 k per second. Perhaps a larger amount of test data is
needed?

Uzytkownik "CountScubula" <me@scantek.hotmail.com> napisal w wiadomosci
news:Zx******************@newssvr25.news.prodigy.c om...
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:NJ********************@comcast.com...
I wonder. According to the PHP manual, flush() is supposed to flush the
server's buffer as well. Presumably the function wouldn't return until it has received an acknowledgement for the last packet sent.

I tried adding ob_start("ob_gzhandler") to the beginning of the file. The measured speed increased nearly ten-fold. So I don't think it's testing the
execution speed.

Hmm, here is an idea, havent done any code, I am going to sleep, diving in
the morning.

what if you were to send a header with a timestamp:

fist call to page:
Location:
http://www.blablabla.com/test.php?action=loop&step=1&ts[1]=microtime

next call to page
Location:

http://www.blablabla.com/test.php?action=loop&step=2&ts[1]=microtime&ts[2]=m icrotime

loop a couple times of times, average out the time stamps for an average
page redirct.

this will be used a reference to delays, overhead etc...

then do this header
Location: http://www.blablabla.com/test.php?ac...avg=_avg_stamp

then for that page, send a lot of header data:
for ($i=0; $i < 100; $i++)
{
$head = "X-SpeedTest: --- speed test $i---";

$headerSize += strlen($head) + 2;
header($head);
}

header("Location:
http://www.blablabla.com/test.php?ac...=mictotime&ds= $headerSize
then the last call to the page:

$timeElapsed = (microtime() - $_GET['ts']) - $_GET['avg'];
$dataSize = $_GET['ds'];

hmm, then I think we would have a size of data sent, and time to send?

just a thought.....What do you think?

--
Mike Bradley
http://www.gzentools.com -- free online php tools

Jul 17 '05 #8

P: n/a
Ok, now try it at:

http://www.gzentools.com/test/speedtest.php

I think it may be off by a factor of 9 to 10, I arrive at this nubmer by
doing a local test, and it shows my DL speed as 5 Mbit/s, and when I do a
ftp download of a large file from the same server, I get about 51 Mbit/s

let me know what everyone thinks, I think we can make this happen!

--
Mike Bradley
http://www.gzentools.com -- free online php tools

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:Ko********************@comcast.com...
Hmmm, using the header might be a better idea than sending the test bytes as HTML comment, as it seems the browser would save the comment tags into the
DOM (thus eat up memory). A bit cleaner too to redirect to a separate page.
Tried your page. It's reporting that my 3 mbit connection is capable of
downloading 1.8 k per second. Perhaps a larger amount of test data is
needed?


Jul 17 '05 #9

P: n/a
On Mon, 19 Jan 2004 23:30:16 GMT, "CountScubula"
<me@scantek.hotmail.com> brought forth from the murky depths:
Ok, now try it at:

http://www.gzentools.com/test/speedtest.php


I get "too many" redirection errors with NN7, Mike.

"Your line speed is around 11.788 Mbit/s" for Stargate
sat modem via IE6.

-
Every day above ground is a Good Day(tm).
-----------
http://diversify.com Website Application Programming
Jul 17 '05 #10

P: n/a
"Larry Jaques" <novalidaddress@di\/ersify.com> wrote in message
news:n2********************************@4ax.com...
On Mon, 19 Jan 2004 23:30:16 GMT, "CountScubula"
<me@scantek.hotmail.com> brought forth from the murky depths:
Ok, now try it at:

http://www.gzentools.com/test/speedtest.php


I get "too many" redirection errors with NN7, Mike.

"Your line speed is around 11.788 Mbit/s" for Stargate
sat modem via IE6.

-
Every day above ground is a Good Day(tm).
-----------
http://diversify.com Website Application Programming


OK, I changed the redirects to a lower number, how is it now?

btw, was 11.788 to high or too low?

http://www.gzentools.com/test/speedtest.php

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #11

P: n/a
On Wed, 21 Jan 2004 02:44:03 GMT, "CountScubula"
<me@scantek.hotmail.com> brought forth from the murky depths:
OK, I changed the redirects to a lower number, how is it now?
NN works now: Your line speed is around 933.794 Kbit/s
Your line speed is around 1.030 Mbit/s
Your line speed is around 7.401 Mbit/s
Your line speed is around 1.062 Mbit/s
IE: Your line speed is around 2.025 Mbit/s
Your line speed is around 447.926 Kbit/s
Your line speed is around 2.340 Mbit/s
Your line speed is around 754.559 Kbit/s
btw, was 11.788 to high or too low?


Good question. Satellites connections are iffy. Sometimes it
takes half an hour to upload 40 files for a site where it
would have taken 5 minutes via my 56k modem. Sometimes I get
2MB download speeds, other times 7MB speeds. The variations
in your program are probably due to the sat latencies.

Are others with true hardwired connections getting more closely
matched results, or widely varying results like mine?
----------------------------------------------------------------
"Let's sing praise to Aphrodite || www.diversify.com
She may seem a little flighty, || Full Service Websites
but she wears a green gauze nighty, || PHP Applications
And she's good enough for me." || SQL Database Development
Jul 17 '05 #12

P: n/a
"Larry Jaques" <novalidaddress@di\/ersify.com> wrote in message
news:ia********************************@4ax.com... > On Wed, 21 Jan 2004
02:44:03 GMT, > >btw, was 11.788 to high or too low?

Good question. Satellites connections are iffy. Sometimes it
takes half an hour to upload 40 files for a site where it
would have taken 5 minutes via my 56k modem. Sometimes I get
2MB download speeds, other times 7MB speeds. The variations
in your program are probably due to the sat latencies.

Are others with true hardwired connections getting more closely
matched results, or widely varying results like mine?
----------------------------------------------------------------
"Let's sing praise to Aphrodite || www.diversify.com

Hmm, I do not know, c'mon people post a little, please.

Btw, I did an ftp download localy, and tweeked script to match that. I
know, rudimentry baseline.
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #13

P: 1
k so i know that this is pretty old but if i were you i would look into using javascript and implement ajax(to be able to load a page behind the scenes) my theory behiond this is you shoul dbe able to open a page in the background then test to see how fast it opened(which your able to do with the readystate of an XMLHttpRequestObject)

I dunno i havent actually tried and it sjust an idea im tossing out there. because the way it seems you gusy are oging about it now is your testing the servers speed(how fast the server can compile your page) because the filesize of a page changes (for example in forums when more posts are added)

To branch off of the Ajax idea, heres how i think you could go about it:

at the end of your php script set $_SESSION["microtime_1"]=microtime(); then flush out your html

then load an image (in the HTML document)(make the file bigger in size) and say <img onLoad="runTimeTest()">

in run test run an XMLHttpRequest that opens another php page behind the scenes to set $_SESSION["microtime_2"]=microtime();

and it will also say something like echo"speed = " . $approximateFileSize/($_SESSION["microtime_2"] - $_SESSION["microtime_1"] );

$approximateFileSize = (this will be the file size of the two pages and the image added togethor)

anyways, i knwo this isnt really that clear and i probobly jumped around alot but i think this might work fo ryou. anyways hope someonee will give this a try(im too lazy lol) that is of course if anyone else is even interested still
Jun 28 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.