Use IP for address query
To use IP for address queries, you need to connect to the GEOIP database. This article will use the simple online service provided by Freegroip.net.
Example: Find the user's geographic location
<? PHP function Show_ip_info ($IP) { //sends a service request and outputs the result, the URL requested by the Web site is in the format of the http://freegeoip.net/{data format}/{the IP address} to query. The data format returned is CSV, XML, or JSON. $url = ' http://freegeoip.net/csv/'. $ip; Open connection $fp = fopen ($url, ' R '); $read = Fgetcsv ($fp); Close connection fclose ($fp); echo " <p> IP Address: $ip </br> Country: $read [2]</br> city,state: $read [5],$ read[3]</br> </p> "; } Show_ip_info ($_server[' remote_addr '); $url = ' www.sdust.com '; Show_ip_info (gethostbyname ($url));?>
Attention:
1. The key to using Web services is to understand what URLs to use and what results to return. For ease of debugging, you can use a browser to load the service to confirm the results returned by the service.
2. IP addresses are not reliable, for example, sometimes multiple users on the same network may look outside to use the same IP address.
3. To obtain a domain name corresponding to an IP address, you can use the gethostbyaddr () function.
4. If a URL points to a site that is running on more than one server, the Gethostbynamel () function returns all IP addresses.
PHP socket programming using IP for address query