开发者

Is there a way to detect if a user is on a wifi connection with php or javascript?

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 开发者_如何学Pythonjavascript, or php?


There are two ways to do this that I know of:

  1. 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.

  2. 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:

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


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.

No, IPv6 doesn't include this information either.


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.


No. Well, you could check their IP against some theoretical database - in a very very limited number of cases it may tell you.

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.


JSFiddle Example

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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜