Credit: This is a modification of the Bermi Labs IP to country web service. Its purposes are to link IPs with ISO 3166-3 country codes, and to link 2 and 3 character country codes with country names.
Like all web services, the input is a URL and the output is a string. In the example
http://www.lakeinfoworks.com/webservices/country/ip/abbr3/1044964400
| http://www.lakeinfoworks.com | The server |
| webservices | The web service's location on the server. |
| country | The name of the PHP program that fetches the data. |
| ip | Describes the input, an IP address |
| abbr3 | Describes the output, a 3 character abbreviation. |
| 1044964400 | The IP address |
Put into your browser and you will get “FIN” echoed. Additional URI elements that may be used for either input or output are
There is one more feature that may be of some use. It is the input “all”. In the example
http://www.lakeinfoworks.com/webservices/country/all
The returned value is all of the countries. The countries are delimited by “\n” characters. Each country is a “|” delimited string consisting of the country's 3 character code, its 2 character code and its name.
-
-
-
-
-
-
-
-
all echos “AFG|AF|Afghanistan\nALB|AL|Albania\nDZA|DZ|Algeria…”. If you demo this option, look at the result by viewing the source.
Within an Akelos program, you may code:
The webservice at Lakeinfoworks should not be used in production sites because your application will break whenever this site goes down or the program fails due to continuing development. Following are instructions for installing this webservice on your server.
Create the “public_html/webservices/country” and “hidden/webservices/country” directories. “public_html” is the Document Root. “hidden” lies outside the Document Root, where a hacker with a browser can't get at it.
Download and unpack
country.zip into the “public_html/webservices” directory.
Move the country.php, and .htaccess files to the “public_html/webservices/country” directory.
Move the config.php file to the “hidden/webservices/country” directory.
Create a database and permissions so that one set can update the database, and the other set can just do SELECTS.
On some servers, a database may have just one set of permissions available. In this case, it's more important to remove the database modifying programs after the tables are populated, as noted below.
Modify config.php with your DB credentials: host, user, password and database name. The development database credentials are for updating; the production database credentials are for reading data from the database. If your server allows only one set of permissions, make both the same.
Modify create_db.php, update.php and country.php so that the include_once for config.php points to the correct location.
-
-
This program uses the database provided by
ip-to-country.webhosting.info. You may wish to download a fresh copy of it for your use. Bermi's program did this automatically, but my remote server wouldn't allow this feature, so I took it out. The ip-to-country.csv file has the country names as all upper case. This program makes them mixed case, but with a couple of problems. One country's name and 3 character code are missing. The
PHP function
ucwords didn't catch some data, such as after a ”.” and after a ”(”. These problems have been repaired in countries.sql. After running update.php, you may want to update your table with this file.
After checking your tables, you may wish to remove the database modifying programs create_db.php and update.php. This will insure that a hacker can't execute them.
Your web service is ready for use.
Remember to cache your visitors results so you don't hit this service twice for the same IP.
After you load country.zip into public_html/webservices and unzip it, you may open index.html with your browser to see these instructions and to make the links useful.
The web service will be changed in the near future to create a replacement for country_select.