10

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?

mcbeav
  • 11,893
  • 19
  • 54
  • 84
  • You need to clarify what do you mean. To check whether a site visitor is using WiFi on their side? To see if a person from your company is connected through WiFi when visiting the corporate site? To check if a person from your network is currently connected to the company WiFi when another person visits your site? What do you actually want to do? Wouldn't using HTTPS for your site resolve the issue? – Rosh Oxymoron Dec 16 '10 at 09:14

5 Answers5

13

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>
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • 3
    failed in: mobile: chrome, default android browser, firefox beta. desktop: aurora, chrome, opera, safari. I copied your html, saved it to a new html document and loaded that. I get "Connection type not supported" every time. – frandroid Apr 03 '14 at 14:26
  • The only reliable way is to check against a database. The navigator.connection stuff isn't widely supported and it isn't even up to date (4G, LTE, etc.). It's definitely possible, but it's likely going to cost you money to get this information. – Tom May 09 '14 at 15:51
11

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.

Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • 7
    This answer is simply incorrect. While there is no way built in to HTTP to determine the connection type, my answer below clearly shows how you can determine which class of connection you are on. Mobile ad companies in particular know exactly what type of connection you are using. There are several companies that compile very comprehensive maps of connection type to ip. – Sky Kelsey Oct 16 '13 at 18:46
1

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.

Thariama
  • 50,002
  • 13
  • 138
  • 166
0

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.

Robert
  • 6,412
  • 3
  • 24
  • 26
  • thanks for the excellent answer! The only reason i wondered PHP, was because i know you can get their IP address, and was wondering if you could somehow do something with it. – mcbeav Dec 16 '10 at 09:13
  • What I meant by "theoretical database" is, if someone like Comcast or Boingo or Waypoint used only IPs in some set of netblocks, those could conceivably be listed somewhere as "public WiFi IPs". But you'd never get the mom-and-pop stores running it off a cable modem, for example, and I doubt the big companies do it anyways. – Robert Dec 16 '10 at 09:17
0

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>
Visal
  • 397
  • 1
  • 3
  • 10
  • Yes Sure. This is the JS Api for finding if user is connected online. It renders Online or Offline in the div. The only problem is that it doesn't show changes if the network connection changes. Sorry if the answer isn't clear. – Visal Jan 03 '20 at 18:08