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