javascript 有没有办法用php或javascript检测用户是否在wifi连接上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4459029/
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
Is there a way to detect if a user is on a wifi connection with php or javascript?
提问by mcbeav
Cant seem to find any info on this, but was wondering if there is any way to detect if a user is on a wifi connection, specifically public wifi, using javascript, or php?
似乎无法找到有关此的任何信息,但想知道是否有任何方法可以检测用户是否使用 wifi 连接,特别是公共 wifi,使用 javascript 或 php?
回答by Arafangion
No, there isn't, there is nothing in the IPv4 nor the HTTP transport that even hints at what kind of connection is used, except for the underlying protocol itself, which is usually IPv4 and HTTP.
不,没有,除了底层协议本身(通常是 IPv4 和 HTTP)之外,IPv4 和 HTTP 传输中没有任何内容甚至暗示使用哪种连接。
No, IPv6 doesn't include this information either.
不,IPv6 也不包含此信息。
回答by Sky Kelsey
There are two ways to do this that I know of:
我知道有两种方法可以做到这一点:
Check the IP against a database. This option gives you much more than carrier information, by the way. It can also give you the location and name of the ISP, the domain that maps to this IP, lat/long, zip code, time zone, etc., etc. Look at http://www.quova.com/for a RESTful API that allows this.
Programmatically: This only works on Android version 2.2+. It is a simple check for navigator.connection. Hope this helps. Here is a test page:
根据数据库检查 IP。顺便说一下,此选项为您提供的不仅仅是运营商信息。它也可以给你的ISP的位置和名称,映射到这个IP,纬度/经度,邮编域,时区,等等等等看http://www.quova.com/了允许这样做的 RESTful API。
以编程方式:这仅适用于 Android 2.2+ 版本。这是对 navigator.connection 的简单检查。希望这可以帮助。这是一个测试页面:
<html>
    <head>
        <script type="text/javascript">
            function checkWIFI() {
                var output = document.getElementById('connectionCheck');
                var html = "Checking...<br/>Connection: ";
                if (navigator.connection) {
                    var type = navigator.connection.type;
                    switch (type) {
                        case navigator.connection.UNKNOWN:
                            html += "Unknown";
                            break;
                        case navigator.connection.ETHERNET:
                            html += "Ethernet";
                            break;
                        case navigator.connection.WIFI:
                            html += "Wifi";
                            break;
                        case navigator.connection.CELL_2G:
                            html += "Cell 2G";
                            break;
                        case navigator.connection.CELL_3G:
                            html += "Cell 3G";
                            break;
                        default:
                            html += "Missing";
                    }
                } else {
                    html += "Connection type not supported.";
                }
                output.innerHTML = html;
            }
        </script>
    </head>
    <body onload="checkWIFI();">
        <div id="connectionCheck">
        </div>
    </body>
</html>
回答by Thariama
You can get some information concerning the user - like IP, used OS, protocol. But you are not able to fetch any information concerning the used medium the user uses to connect to the internet. There are some speed testing tools to analize the connection speed with which a user is connected, but wireless connections do not have any significant speed mark or whatever to identify it.
您可以获得有关用户的一些信息 - 例如 IP、使用的操作系统、协议。但是您无法获取有关用户用于连接到 Internet 的所用媒体的任何信息。有一些速度测试工具可以分析用户连接的连接速度,但无线连接没有任何显着的速度标记或任何识别它的东西。
回答by Coder
if (navigator.onLine) { //Works in most browsers. Not all.
  document.getElementById("online").innerText = "Online";
} else {
  document.getElementById("online").innerText = "Offline";
}
<html>
  <div id="online"></div>
</html>
回答by Robert
No. Well, you could check their IP against some theoretical database - in a very very limited number of cases it may tell you.
不。好吧,您可以根据一些理论数据库检查他们的 IP - 在非常有限的情况下,它可能会告诉您。
But Javascript by design doesn't expose enough about the user, and there's definitely no way for PHP to know unless it can somehow convince the client to share.
但是 Javascript 的设计并没有足够地暴露用户,而且 PHP 肯定没有办法知道,除非它可以以某种方式说服客户端共享。

