php 如何在PHP中检测互联网速度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5529620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect internet speed in PHP?
提问by Sharon Haim Pour
How can I create a PHP page that will detect the user's internet speed and show it on the page? Something like,
如何创建一个 PHP 页面来检测用户的互联网速度并将其显示在页面上?就像是,
Your internet speed is ??? Kbps
你的网速是???传输速率
采纳答案by cabaret
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.
此脚本向您的客户端发送 512 KB 的 HTML 注释。解析该 HTML 可能会增加总传输时间,因此不要将其视为原始下载速度。
Quoted from: PHP Speed test
引用自:PHP 速度测试
Source is here:
来源在这里:
http://jan.moesen.nu/code/php/speedtest/index.php?source=1
http://jan.moesen.nu/code/php/speedtest/index.php?source=1
Hope that helps.
希望有帮助。
回答by Martin
<?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";
?>
回答by Olli
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.
例如通过在客户端计时 AJAX 请求。这样您就可以计算出大致的下载速度,但不能计算出上传速度。对于上传,发送大型 AJAX POST 请求可以处理它。
With jQuery and $.ajax
it's pretty trivial to do.
使用 jQuery$.ajax
做起来非常简单。
回答by Dejan Marjanovic
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 为单位)与时间(以秒为单位)进行划分。然后您将获得 kb/s(上传速度)。
$kb = round(filesize("file.jpg") / 1024); // 500kb
$time = time() - $start; // 5s
$speed = round($kb / $time); // 100kb/s
回答by Anand Singh
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.
我从这里得到了这个。
回答by bl00dshooter
Not really possible. PHP is server sided, detecting speed would be client sided.
不太可能。PHP 是服务器端的,检测速度是客户端的。
You may find work arounds to do it, tho.
您可能会找到解决方法来做到这一点,不过。