This is a simple and funny way to integrate MySQL, PHP, XML and Flash.
A basic understanding of these topics is required, as I won't go into too many details.
This is all based on the .csv file available at http://ip-to-country.webhosting.info/. You can download the file here.
As the countries' flags are also available at http://ip-to-country.webhosting.info/node/view/91, I use them as well. I converted all of them (using Fireworks MX 2004) to .jpg, as it's the only format that can be loaded into Flash.
The first step is creating a database, and then using a PHP script to load all the info contained in the .csv file into the database's table.
This very same script is also used to update the database, as before it starts introducing the new values, it deletes all the existing ones.
I created two PHP files and two Flash movies - one that outputs XML and another that only outputs variable name/value pairs.
The flash file is very simple: two or three text fields respectively, and an empty movie clip - everything created dynamically. The flash file triggers the PHP script, and then loads the respective content.
Here's an example:
You should see your country's name and flag. If this doesn't happen, it's likely that the database is out of date and doesn't contain your IP address yet.
You can download all the files here.
Creating the table:
-
CREATE TABLE iptocountry (
-
ip_from double DEFAULT NULL,
-
ip_to double DEFAULT NULL,
-
country_code2 char(2) DEFAULT NULL,
-
country_code3 char(3) DEFAULT NULL,
-
country_name varchar(50) DEFAULT NULL
-
) TYPE=MyISAM;
This code creates a table called iptocountry - in the currently selected database (test) - with 5 columns, e.g.:
- ip_from - [33996344]
- ip_to - [33996351]
these two values provide the ip range - [33996344 to 33996351] - country_code2 (the country code with two characters) - [GB]
- country_code3 (the country code with three characters) - [GBR]
- country_name - [UNITED KINGDOM]
Inserting values in the table
Like said before, a PHP script reads the .csv file, and inserts the values into the table:
-
<?php
-
// connect to the database
-
-
// delete existing records
-
$query = "DELETE FROM iptocountry";
-
-
// open the .csv file for reading
-
-
-
// read each line of the text file
-
-
// transform each line into an array
-
-
// attention - there are country names with single quotes: COTE D'IVOIRE
-
// which require the use of addslashes()
-
-
// store the useful values in the table iptoucoutry
-
$query = "insert into iptocountry VALUES ('$data[1]','$data[3]','$data[5]','$data[7]','$data[9]')";
-
}
-
-
// close the connection to the database
-
// close the file
-
?>
PHP output - XML
Another PHP script queries the database, and outputs the results in XML format:
-
<?php
-
// get user's ip address
-
$ip = $_SERVER['REMOTE_ADDR'];
-
// change the ip's format
-
-
// connect to the database
-
-
// query the database
-
// find the country_name and the country_code2 inside the specified ip range
-
// select only one value
-
$query = "SELECT country_name, country_code2 FROM iptocountry WHERE ip_from <= $ip_new && ip_to>= $ip_new LIMIT 1";
-
-
// output the result in XML format
-
echo '<?xml version="1.0" encoding="UTF-8"?>';
-
echo '<country>';
-
echo '</country>';
-
?>
PHP output - variables
Alternatively, a similar script outputs variable name/value pairs:
-
<?php
-
// exactly the same as above, except for the XML part
-
-
$country = $row['country_name'];
-
$country_2 = $row['country_code2'];
-
-
// output the variables
-
echo "&name=$country&code=$country_2&ip=$ip";
-
?>
The only thing missing is the Flash movie and the corresponding code:
Flash movie - XML
-
// the url that you use both to the scrip and to the flags' folder
-
var url = "http://localhost/php/";
-
/*
-
create the text fields
-
for the country name and code
-
*/
-
this.createTextField("coutry_txt", 1, 0, 0, 100, 40);
-
this.createTextField("code_txt", 2, 0, 50, 100, 40);
-
/*
-
create an empty movie clip to hold the flag
-
and position it
-
*/
-
this.createEmptyMovieClip("flag_mc", 3);
-
flag_mc._y = 100;
-
/*
-
create the my_xml XML Object
-
to hold the xml data created by the php script getCountryXML.php
-
*/
-
var my_xml = new XML();
-
my_xml.ignoreWhite = true;
-
// when the xml loads show the country name and code, and the flag
-
my_xml.onLoad = function(success)
-
{
-
if (success) {
-
var temp_xml = this.firstChild;
-
trace(temp_xml);
-
// country name
-
var name_xml = temp_xml.childNodes[0].attributes.name;
-
trace(name_xml);
-
// country code
-
var code_xml = temp_xml.childNodes[0].attributes.code;
-
trace(code_xml);
-
// show text:
-
coutry_txt.text = "your country is:\n" + name_xml;
-
code_txt.text = "the country code is:\n" + code_xml;
-
// show flag
-
flag_mc.loadMovie(url + "flags/" + code_xml.toLowerCase() + ".jpg");
-
// an error occurred
-
} else {
-
trace("an error occurred");
-
}
-
};
-
my_xml.load(url + "getCountryXML.php");
Flash movie - variables
Similarly, this code loads the variables:
-
var url = "http://localhost/";
-
-
/*
-
create the text fields
-
for the country name and code and ip
-
*/
-
-
this.createTextField("coutry_txt", 1, 0, 0, 100, 40);
-
this.createTextField("code_txt", 2, 0, 50, 100, 40);
-
this.createTextField("ip_txt", 4, 0, 120, 100, 40);
-
-
this.createEmptyMovieClip("flag_mc", 3);
-
flag_mc._y = 100;
-
-
/*
-
create the my_lv LoadVars Object
-
to hold the variables that come from the php script getCountry.php
-
*/
-
-
var my_lv = new LoadVars();
-
// when the variables load, show the country name and code, and the flag
-
my_lv.onLoad = function(success)
-
{
-
if (success) {
-
// country name
-
trace(this.name);
-
// country code
-
trace(this.code);
-
// ip
-
trace(this.ip);
-
-
// show text:
-
coutry_txt.text = "your country is:\n" + this.name;
-
code_txt.text = "the country code is:\n" + this.code;
-
ip_txt.text = "the ip is:\n" + this.ip;
-
-
// show flag
-
flag_mc.loadMovie(url + "flags/" + this.code.toLowerCase() + ".jpg");
-
// an error occurred
-
} else {
-
trace("an error occurred");
-
}
-
};
-
my_lv.load(url + "getCountry.php");