从浏览器而不是服务器测量 Ping 时间?
我如何测量从用户浏览器到 IP 地址的 ping 时间,而不是从服务器到 IP 地址的 ping 时间。我希望能够在网站上获取并显示 ping 时间,该网站显示从该人的浏览器到 IP 地址的 ping 时间。
我见过的所有代码都可以 ping 通,但如果我没记错的话,它显示的是从服务器到 IP 地址的时间,而不是从客户端浏览器或 PC 到 IP 地址的时间。
我也尝试过在 PHP 中加载图像并测量加载所需的时间(我认为!),这可能是一个选项,但我不知道在哪里测量时间。在服务器上还是在浏览器上?
<html>
<head>
<title>Psedo Ping Test</title>
</head>
<body>
<?php
$link2 = "http://www.is.co.za/Style%20Library/en-is/Themable/Images/ISlogo.jpg";
echo fopenTest($link2)
?>
</body>
</html>
<?php
function fopenTest($link) {
if(substr($link,0,4)!="http"){
$link = "http://".$link;
}
$timestart = microtime();
$churl = @fopen($link,'r');
$timeend = microtime();
$diff = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) - (substr($timestart,0,9)) - (substr($timestart,-10))),4);
$diff = $diff*100;
if (!$churl) {
$message="Offline";
} else {
$message="Online. Time : ".$diff."ms ";
}
fclose($churl);
return $message;
}
?>
How would I measure a ping time from the users browser to an IP address and not from the server to th IP address. I want to be able to get and show the ping times on a website that shows the ping times from that persons browser to an IP address.
All the code I've seen can ping, but if I'm not mistaken, it shows the time from the server to the IP address and not from the clients browser or PC to the IP address.
I've tried also loading an image in PHP and measuring the time it takes to load (I think!) and this might be an option but I don't know where the time is being measured. On the server or on the browser?
<html>
<head>
<title>Psedo Ping Test</title>
</head>
<body>
<?php
$link2 = "http://www.is.co.za/Style%20Library/en-is/Themable/Images/ISlogo.jpg";
echo fopenTest($link2)
?>
</body>
</html>
<?php
function fopenTest($link) {
if(substr($link,0,4)!="http"){
$link = "http://".$link;
}
$timestart = microtime();
$churl = @fopen($link,'r');
$timeend = microtime();
$diff = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) - (substr($timestart,0,9)) - (substr($timestart,-10))),4);
$diff = $diff*100;
if (!$churl) {
$message="Offline";
} else {
$message="Online. Time : ".$diff."ms ";
}
fclose($churl);
return $message;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个。它主要与您的 PHP 示例相同。但用 JavaScript 完成。
Take look at this. It's mainly the same as your PHP example. But done in Javascript.