PHP socket programming using IP for address query

Source: Internet
Author: User
Tags geoip database

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.