- Community Pick
The IP address can usually be found in $_SERVER["REMOTE_ADDR"]. In most PHP configurations this is reliable, but please see the man page notes about relying solely on the values in $_SERVER.
http://www.php.net/manual/
This reference may be useful, too.
http://www.php.net/manual/
1. ACQUIRE THE IP-TO-COUNTRY DATA
Our first step is to acquire a data base of IP ranges and country identifications. You can download a CSV file with this information from MaxMind here:
http://geolite.maxmind.com
It will look something like this:
12.109.8.112, 12.109.8.119, 208472176, 208472183, PR, Puerto Rico
12.109.8.120, 12.129.198.255, 208472184, 209831679, US, United States
12.129.199.0, 12.129.199.31, 209831680, 209831711, DE, Germany
The first two fields are IP string ranges, also called the IP address. The next two fields are the corresponding IP numbers. The last two fields are the country abbreviation and the country name. On July 1, 2010 the file had a bit less than 130,000 rows. The file will be about 9MB after it is unzipped.
Unzip the file and store it on your server in the web root - let's call it 'GeoIPCountryWhois.csv' for now. You might attach the current date to the file name, in case you are interested in keeping track of versions. MaxMind issues a new release of the file every month.
2. LOAD THE IP-TO-COUNTRY INTO A DATA BASE
The second step is to load the data base using the contents of the CSV file.
Run the "LOAD" script from the same directory as the 'GeoIPCountryWhois.csv' data. On my server it took about 15 seconds to run. As you look over the code, you can see that this script does the following things.
1. We set a variable with the name of the file we downloaded from MaxMind - that is our source of input data (line 11).
2. We connect and select our data base (lines 14 - 36).
3. We discard any old version of the "ip2country" table (line 36).
4. We create a new table definition (lines 40 - 60). Experienced data base users will notice that we have no index definitions in our CREATE TABLE statement. Indexes improve performance during SELECT queries but are of no value (and may even slow down) INSERT statements, so we create the table without indexes, and we add them later after the data has been loaded.
5. We open and read the CSV file, performing some modest edits, and use INSERT to add each record to the "ip2country" table (lines 62 - 106).
6. Finally, we add the indexes to the table on the ip_number columns. This will make table lookups very fast.
3. CREATE A "RESTful" WEB SERVICE
Now that our table is loaded and indexed for fast performance, we can look up IP addresses and return the country data. We will demonstrate this ability by creating a RESTful web service. REST services use the GET string (URL) to communicate with the service. The input argument is the IP address and the response is a short CSV string with the country code and country name. We could modify this service to return XML or JSON, but simplicity can be a virtue.
Our REST service connects to the data base so it can use the table we created in the earlier step. You may notice that the error messages are very terse - since this is a service that is designed to be simple to use, we only return one string of data. If the string starts with the word, "ERROR" it indicates a failure. Otherwise the string will have two fields separated by a comma: The country code and the country name. For example, a visitor from "down under" would be identified with a CSV string that said "AU,Australia"
You can integrate this service into your web applications with a single line of code that sends the IP address to the URL of the REST script. Here are some sample calls to the service:
// OUTPUT THE CLIENT'S COUNTRY
echo file_get_contents("path/to
// OUTPUT THE COUNTRY THAT HAS IP#123.45.67.89
echo file_get_contents("path/to
// OUTPUT AN ERROR MESSAGE
echo file_get_contents("path/to
4. PULLING IT ALL TOGETHER
Now that you know how to find the client's country, you can do a number of interesting things with the information.
You might keep a record of where your site visitors come from. Perhaps you could create a map of the world showing the countries that have visitors to your site. (Google does this with its Analytics service).
You could consider localization options, such as adding a national flag to the page banner, or suggesting a second language for the web site text.
You might be able to access specialized RSS feeds for weather or news based on the name or country code of your visitor's country.
You could provide links to Wikipedia articles in the appropriate language, for example (in Germany):
http://de.wikipedia.org/wi
You could even provide a link to the CIA fact book for that country!
by: Medo3337 on 2010-12-16 at 11:41:12ID: 22122