Detect user connection 3g,4g,wifi or lite by using php
Asked Answered
O

3

6

Is there any way to detect connection of user by using PHP script. Want to detect 3g,WIFI,4g and Lite. I want to detect where the users come from. I already have a solution in JavaScript but I want it in PHP for some reasons.

Overweary answered 26/4, 2014 at 9:43 Comment(4)
I don't think that's possible. Why would the client machine tell you? What is the javascript solution?Simms
In JavaScript I find this navigator.connection Like we can find clients IP by using PHP that's why I was thinking that is there any solution to get his connection type.Overweary
and that's only in firefox.Grime
You can get the user's IP address (or at least some IP belonging to an onion routing network that the user is in) in PHP. Not sure how much it tells you, though. In case of TOR IP addresses... not much.Simms
E
17

You can sometimes tell the difference between WIFI and non-WIFI, and sometimes you can't tell any difference.

You can never tell the difference between 3G/4G/LTE with php.

Assumptions and terminology

I am assuming that a smartphone is connecting to your HTTP server via a browser, and your server uses PHP to handle the request. If that is not the case, please clarify your question.

Terminology: I will use the word "ISP" to refer to companies which provide Internet service but are not phone carriers.

What info does the server get?

When your device connects to a PHP server, it tells the server some info about itself, the server can know the device's IP address, user agent, etc.

The problem

The problem is that the browser "Doesn't care" what connection it is on, and it has no effect on the info the browser sends to the server. If the browser is on 3G/4G/LTE, it would send the exact same info it would have sent if it were on Wifi.

Semi "Proof": The HTTP protocol which transfers the data from the device to the HTTP server is completely ignorant of the underlying network stack.

How do we identify the connection, then?

The only difference between a Wifi/Non-Wifi is: The IP address.

ISP's have a big list of reserved IP addresses. If you can detect that this IP address belongs to a particular ISP, this must be a Wifi connection.

Similarly, phone carriers have a big list of reserved IP addresses. If you can detect that this IP address belongs to a particular phone carrier, this must be a 3G/4G/LTE connection!

Doing it in practice

We need to somehow correlate each IP address to an ISP/Carrier, one way to do this is to have a huge database of all ISP's / Carriers and their respective reserved IP's. In practice, a simple WHOIS lookup can reveal the source.

For example, a WHOIS lookup for a random IP: 109.168.97.31 revealed among other things:

descr: 013 Netvision

Netvision is an Israeli Internet provider. Therefore, if this is a phone connection (You can check that using the user-agent), this must be a Wifi connection!

Doing it in PHP

I have a little knowledge of PHP, perhaps someone with better knowledge will better edit this section. The code probably goes like this:

$company_name = geoip_isp_by_name($_SERVER['REMOTE_ADDR']);
$this_is_a_wifi_connection = is_this_wifi($company_name);

The function is_this_wifi should lookup the company name in a database and decide whether this is a phone carrier or an ISP company.

Problems

  • You still need to have a list of ISP's / Carriers in order to compare your WHOIS lookups with them.

  • Some people use VPN's / Anonymity networks, an IP address can lie.

  • Some companies are both ISP's and carriers, you will never know whether or not it's a Wifi connection in such a case.

Embarkment answered 26/4, 2014 at 10:2 Comment(3)
May be I should initiate this as a project on github.Overweary
Wikipedia hosts a (partial) list of mobile network operatos. It could be a good start: en.wikipedia.org/wiki/List_of_mobile_network_operatorsGraig
We use ip2location service for geolocate IP addresses.Yehudit
E
0

One somewhat possible solution is using geoip_isp_by_name php function to get the name of ISP and then through some API or scraping check what technologies this ISP is offering.

Excoriation answered 26/4, 2014 at 10:0 Comment(0)
E
0

It's not possible in PHP or any other server side language. You have to get that kind of information from client (browser) and currently there is no standard HTTP header which browsers can use it to pass connection type information (List of HTTP header fields).

Browser might know connection type and you might be able to get that information using client side scripts (i.e. javascript) or plugins.

Episcopalian answered 26/4, 2014 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.