crazyflx
Elite Member
- Nov 9, 2009
- 1,623
- 4,870
Before I dive into this, I want to let you know a few things ahead of time.
1.) This is VERY easy to set up, provided you follow the instructions below step by step.
2.) Looking at all of the text below makes it look FAR more difficult than it actually is, but that is only because I am very thorough (almost to a fault) when it comes to explaining things.
3.) I should also mention I set this up on shared hosting, and the host I set up the example below on, was with JustHost.com
Why would you want to run a geotargeting script on your own server? Well, you have full control over it, that's why! If you plan on geotargeting CPA offers or want to cloak a site based on a visitors general geographic location or any other use that is going to require a large number of queries, you're going to need a geolocation script running on your own server. Places like maxmind or any of the numerous other sites that offer "free" services either limit the number of queries you can make, or might not have the specific variable you're looking to have returned to you. Not only that, but if you don't control the server the script is running on, you don't have control over how well your script is working, I mean, what happens if the free service you're using does down? You're screwed!
If you'd like to see what the final result will yield, you can see my fully installed/up & running script here:
http://freebiefindssite.info/iplocation/ip_query.php
Anyway, here we go...first, for the things you'll need. Download the package below:
VirusTotal Link:
VirusTotal Results:
File name: iplocation.rar
Submission date: 2011-05-04 15:32:54 (UTC)
Current status: finished
Result: 0/ 42 (0.0%)
Download Link:
In that package is the IP Database as provided by
as well as a tool that allows you to upload massive mySQL databases that you otherwise wouldn't be able to upload using an interface like PhpMyAdmin. That tool is called "BigDump", and was downloaded from:
Now that you know what you've got, we can actually get started.
First, go ahead and unzip that file you downloaded above onto your hard drive. You'll see a folder named "iplocation". For my example, I'll assume you've unzipped the file onto your desktop, which means there will be a folder named "iplocation" on your desktop.
Upload that folder as is somewhere on your server. It doesn't matter where, just as long as it is accessible. Throughout this example, I'll assume it has been uploaded in the root directory of your site (which means you'd be able to access that folder by visiting this URL: http://yoursite.com/iplocation )
Now you'll need to create a mySQL database on your server, name it whatever you want. I won't go into how you do this, as anybody who has ever installed wordpress before should know how to do this, and it's very likely that 90%+ of the people reading this have installed wordpress before. Make sure you note all the credentials of the database (the database name, the username for it as well as the password).
For my example, I'll say that my newly created database credentials are:
Database Username: crazyflx
Database Name: iplocate
Database Password: blackhatworld
Next, you'll need to build the mySQL databases by executing the uploaded .sql files. Don't worry, it's a snap.
You'll need to open up on your desktop the following file (it is located inside the folder you unzipped onto your computer) -> iplocation\db\bigdump.php Open it using notepad so you can edit it.
Edit the following lines that are within that file, using your database credentials. For the example below, I'm going to put my "imaginary" details in here, but you'll need to enter YOURS, and you should have them noted somewhere from one of the steps above:
Then, just beneath that, edit the following line so it reads EXACTLY like this:
Once you've made both of the edits above, save the file, and re-upload bigdump.php so you overwrite the original one you uploaded.
Then, visit the following URL in the navigation bar of your browser:
http://yoursite.com/iplocation/db/bigdump.php
If you entered everything correctly, you should see the following message on your screen:
Click the "Start Import]" link. It's going to take a bit, as it is executing a very large number of queries to build the database. Once completed, you'll see the following message:
Make sure you delete bigdump.php from your server after the above step has been completed. This is an important step, so please do no forget this.
Now, you need to execute one more mySQL file, but you're not going to use "bigdump.php" this time. You're going to use PhpMyAdmin. So navigate to the cpanel of your server & open up PhpMyAdmin, and then navigate to the database you created for this script (in this example, that database is "iplocate"). Once you're there, click on "import". On the screen that opens, you'll see: "Location of the text file" and then a "Browse" button. Click that button, and you'll need to select the sql file to import into the database.
The file you'll be selecting, is located in the folder you unzipped earlier, at this location: iplocation\db\timezone.sql (you'll be selecting the timezone.sql file).
Once you've selected the file, click on "go" in the bottom right hand corner of the screen. Wait a few minutes, and you'll see the following message:
Now your database is all set up, and we're almost ready to rock 'n roll. All that's left now, is editing one more file and re-uploading it, and then you're finished!
Back on your desktop, open the following file using notepad (it is located inside the folder you unzipped onto your computer) -> iplocation\ip_query.php
Edit the following lines within that file (they'll look like this when you open the file):
Obviously, you need to put in your credentials where the above is highlighted in red.
Once you have edited the file as designated above, re-upload the file to your server so that you overwrite the one that is already on your server.
Want to see what you've done? Visit the following URL in your nav bar:
You'll see it spit out onto your screen the following data:
Country Code, Country Name, Region Code, Region Name, City, Zip Postal Code, Latitude, Longitude
(Here is a link to my site that has the above script working, so you can see what your final results will be: http://freebiefindssite.info/iplocation/ip_query.php )
If you'd like to test it with other ip addresses, you can put the URL into your nav bar like this: http://yoursite.com/iplocation/ip_query.php?ip=1.2.3.4 (where 1.2.3.4 would be an IP address)
For those of you wondering how you'd implement this, here is an example using PHP:
Here are the variables you can use (it's self explanatory what each one is):
1.) This is VERY easy to set up, provided you follow the instructions below step by step.
2.) Looking at all of the text below makes it look FAR more difficult than it actually is, but that is only because I am very thorough (almost to a fault) when it comes to explaining things.
3.) I should also mention I set this up on shared hosting, and the host I set up the example below on, was with JustHost.com
Why would you want to run a geotargeting script on your own server? Well, you have full control over it, that's why! If you plan on geotargeting CPA offers or want to cloak a site based on a visitors general geographic location or any other use that is going to require a large number of queries, you're going to need a geolocation script running on your own server. Places like maxmind or any of the numerous other sites that offer "free" services either limit the number of queries you can make, or might not have the specific variable you're looking to have returned to you. Not only that, but if you don't control the server the script is running on, you don't have control over how well your script is working, I mean, what happens if the free service you're using does down? You're screwed!
If you'd like to see what the final result will yield, you can see my fully installed/up & running script here:
http://freebiefindssite.info/iplocation/ip_query.php
Anyway, here we go...first, for the things you'll need. Download the package below:
VirusTotal Link:
Code:
http://www.virustotal.com/file-scan/report.html?id=7d0c881d99d0da8963baf98607decc7cf1d9be7fa0a94d99e47b2bd47f91a5da-1304523174
VirusTotal Results:
File name: iplocation.rar
Submission date: 2011-05-04 15:32:54 (UTC)
Current status: finished
Result: 0/ 42 (0.0%)
Download Link:
Code:
http://www.multiupload.com/QXJ1FE693L
In that package is the IP Database as provided by
Code:
http://ipinfodb.com
Code:
http://www.ozerov.de/bigdump/
Now that you know what you've got, we can actually get started.
First, go ahead and unzip that file you downloaded above onto your hard drive. You'll see a folder named "iplocation". For my example, I'll assume you've unzipped the file onto your desktop, which means there will be a folder named "iplocation" on your desktop.
Upload that folder as is somewhere on your server. It doesn't matter where, just as long as it is accessible. Throughout this example, I'll assume it has been uploaded in the root directory of your site (which means you'd be able to access that folder by visiting this URL: http://yoursite.com/iplocation )
Now you'll need to create a mySQL database on your server, name it whatever you want. I won't go into how you do this, as anybody who has ever installed wordpress before should know how to do this, and it's very likely that 90%+ of the people reading this have installed wordpress before. Make sure you note all the credentials of the database (the database name, the username for it as well as the password).
For my example, I'll say that my newly created database credentials are:
Database Username: crazyflx
Database Name: iplocate
Database Password: blackhatworld
Next, you'll need to build the mySQL databases by executing the uploaded .sql files. Don't worry, it's a snap.
You'll need to open up on your desktop the following file (it is located inside the folder you unzipped onto your computer) -> iplocation\db\bigdump.php Open it using notepad so you can edit it.
Edit the following lines that are within that file, using your database credentials. For the example below, I'm going to put my "imaginary" details in here, but you'll need to enter YOURS, and you should have them noted somewhere from one of the steps above:
Code:
// Database configuration
$db_server = 'localhost';
$db_name = '[COLOR="red"]iplocate[/COLOR]';
$db_username = '[COLOR="red"]crazyflx[/COLOR]';
$db_password = '[COLOR="Red"]blackhatworld[/COLOR]';
Then, just beneath that, edit the following line so it reads EXACTLY like this:
Code:
$filename = '[COLOR="red"]ipinfodb_one_table_small.sql[/COLOR]'; // Specify the dump filename to suppress the file selection dialog
Once you've made both of the edits above, save the file, and re-upload bigdump.php so you overwrite the original one you uploaded.
Then, visit the following URL in the navigation bar of your browser:
http://yoursite.com/iplocation/db/bigdump.php
If you entered everything correctly, you should see the following message on your screen:
Code:
[COLOR="Cyan"]Start Import[/COLOR] from ipinfodb_one_table_small.sql into YOUR DATABASE NAME at localhost
Note: The current mySQL connection charset is latin1. Your dump file must be encoded in latin1 in order to avoid problems with non-latin characters. You can change the connection charset using the $db_connection_charset variable in bigdump.php
Click the "Start Import]" link. It's going to take a bit, as it is executing a very large number of queries to build the database. Once completed, you'll see the following message:
Code:
[COLOR="Lime"]Congratulations: End of file reached, assuming OK[/COLOR]
Make sure you delete bigdump.php from your server after the above step has been completed. This is an important step, so please do no forget this.
Now, you need to execute one more mySQL file, but you're not going to use "bigdump.php" this time. You're going to use PhpMyAdmin. So navigate to the cpanel of your server & open up PhpMyAdmin, and then navigate to the database you created for this script (in this example, that database is "iplocate"). Once you're there, click on "import". On the screen that opens, you'll see: "Location of the text file" and then a "Browse" button. Click that button, and you'll need to select the sql file to import into the database.
The file you'll be selecting, is located in the folder you unzipped earlier, at this location: iplocation\db\timezone.sql (you'll be selecting the timezone.sql file).
Once you've selected the file, click on "go" in the bottom right hand corner of the screen. Wait a few minutes, and you'll see the following message:
Code:
[COLOR="Lime"]Import has been successfully finished, 104 queries executed.[/COLOR]
Now your database is all set up, and we're almost ready to rock 'n roll. All that's left now, is editing one more file and re-uploading it, and then you're finished!
Back on your desktop, open the following file using notepad (it is located inside the folder you unzipped onto your computer) -> iplocation\ip_query.php
Edit the following lines within that file (they'll look like this when you open the file):
Code:
//connect to db
$con = mysql_connect('localhost', '[COLOR="red"]YOUR DATABASE USERNAME HERE[/COLOR]', '[COLOR="red"]YOUR DATABASE PASSWORD HERE[/COLOR]') or die(mysql_error());
mysql_select_db('[COLOR="red"]YOUR DATABASE NAME HERE[/COLOR]', $con);
Obviously, you need to put in your credentials where the above is highlighted in red.
Once you have edited the file as designated above, re-upload the file to your server so that you overwrite the one that is already on your server.
YOU'RE FINISHED!!
Want to see what you've done? Visit the following URL in your nav bar:
Code:
http://yoursite.com/iplocation/ip_query.php
You'll see it spit out onto your screen the following data:
Country Code, Country Name, Region Code, Region Name, City, Zip Postal Code, Latitude, Longitude
(Here is a link to my site that has the above script working, so you can see what your final results will be: http://freebiefindssite.info/iplocation/ip_query.php )
If you'd like to test it with other ip addresses, you can put the URL into your nav bar like this: http://yoursite.com/iplocation/ip_query.php?ip=1.2.3.4 (where 1.2.3.4 would be an IP address)
For those of you wondering how you'd implement this, here is an example using PHP:
Code:
<?php
//connect to db
$con = mysql_connect('localhost', 'DATABASE USERNAME', 'PASSWORD') or die(mysql_error());
mysql_select_db('DATABASE NAME', $con);
//Get the parameters
if ($_GET["ip"]){
$ip = mysql_real_escape_string(trim($_GET["ip"]), $con);
} else {
$ip = getenv('REMOTE_ADDR');
}
//Run the IP location query
$ipQuery = mysql_query("SELECT * FROM `ip_group_city` where `ip_start` <= INET_ATON('$ip') order by ip_start desc limit 1;", $con);
$ipData = mysql_fetch_array($ipQuery);
$nbResults = (bool)mysql_num_rows($ipQuery);
[COLOR="Lime"]$CountryCode = $ipData['country_code'];[/COLOR]
[COLOR="Lime"]if ( $CountryCode == US[/COLOR] ) {
echo "You're from the United States, Nice Flag!";
} else {
echo "You're outside the United States, do you like the way the US Flag looks?";
}
?>
Here are the variables you can use (it's self explanatory what each one is):
Code:
$CountryCode = $ipData['country_code'];
$CountryName = utf8_encode($ipData['country_name']);
$RegionCode = $ipData['region_code'];
$RegionName = utf8_encode($ipData['region_name']);
$City = utf8_encode($ipData['city']);
$ZipPostalCode = $ipData['zipcode'];
$Latitude = $ipData['latitude'];
$Longitude = $ipData['longitude'];
Last edited: