1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Tutorial] How to Set Up Your Own GeoTargeting Script

Discussion in 'Black Hat SEO Tools' started by crazyflx, May 4, 2011.

  1. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    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:
    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
    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:
    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'];
     
    • Thanks Thanks x 34
    Last edited: May 5, 2011
  2. pathart

    pathart Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    845
    Likes Received:
    653
    Location:
    Dublin - Ireland
    Thanks a lot for this , I'm going to add this to one of my ecommerce sites hopefully this will improve my conversions.

    Cheers !
     
  3. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    No problem at all, if you have any questions, just post 'em on the thread.
     
  4. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    For anybody who was interested in this, but was waiting for the VirusTotal, the VirusTotal site was down last night when I posted this thread, but is now back up, and the VT results are now in the OP.
     
    • Thanks Thanks x 1
  5. Rasputin78

    Rasputin78 Regular Member

    Joined:
    Sep 14, 2010
    Messages:
    376
    Likes Received:
    82
    Gender:
    Male
    Location:
    Ether
    WOW Thanks for taking time out to write this tut in such detail dude , I was using a crappy georedirect , But this looks way better.

    Thanks again :)

    EDIT: Almost done setting this up , I have a question can this be used to redirect people from multiple countries to multiple pages ?

    EG US IP >> Link 1

    CA IP>> LInk 2 is this possible?
     
    • Thanks Thanks x 1
    Last edited: May 4, 2011
  6. ch8878

    ch8878 Elite Member

    Joined:
    Mar 21, 2009
    Messages:
    2,242
    Likes Received:
    428
    Gender:
    Male
    Occupation:
    Gamer
    Location:
    Youtube
    Home Page:
    Do you get a lot of traffic using this ?
     
  7. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Yes, that is absolutely possible, and quite easy. Once you've got the script set up and working, post on this thread and I'll throw together a quick example for you on how to redirect users from certain geo-locations to other places.

    I'm not sure what you mean, this thread doesn't really have anything to do with generating traffic.
     
  8. Rasputin78

    Rasputin78 Regular Member

    Joined:
    Sep 14, 2010
    Messages:
    376
    Likes Received:
    82
    Gender:
    Male
    Location:
    Ether
    Awesome man , am done could u be kind enough to make a simple script and explain how to use it ? I would like something which will redirect to say 5 different sites/pages based on IP/country. And maybe 1 page for all other countries.

    Eg: US>>> Link 1

    CA: >> Link 2

    Rest of the world>>> Link 3

    Thanks again :)
     
    Last edited: May 5, 2011
  9. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Happy to see you got it set up. Was the tutorial thorough enough, I mean, were you left confused with anything or did you breeze through it no problem?

    As for a simple script to do what you asked, sure, no problem at all. Here you go (working example of the exact script below is located here: http://freebiefindssite.info/redirect.php ):

    Code:
    <?php
    
    //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);
     
    //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);
                         
    $CountryCode = $ipData['country_code'];
    
    
    [COLOR="red"]if ($CountryCode == 'US'){header('Location: http://www.flags.net/UNST.htm');}
    else if ($CountryCode =='DE'){header('Location: http://www.flags.net/GERM.htm');}
    else if ($CountryCode =='CA'){header('Location: http://www.flags.net/CANA.htm');}
    else {header('Location: http://blackhatworld.com');}[/COLOR]
    ?>
    
    Simply copy and paste the code above into notepad, and save it as whatever you want, with a php extension. So, for example: redirect.php but, as I said, you can (and should) name it something a little less conspicuous.

    As you can see, if the user is located in the US, they are redirected to a site that shows the US flag. If they are from Germany, to a site with a German flag and then lastly, if they are from Canada, to a site showing a Canadian flag.

    (You can test it to see that it works, by visiting the above test URL using this proxy browsing site, which is located in Germany: unblockbypass.com )

    You would simply change the URLs to redirect them to whatever you see fit (internal or external URL).

    The last option you see there, where it says blackhatworld.com, is the URL they are redirected to if the country code returned by the script doesn't have a defined location. In other words, if somebody was visiting from Asia to the script above, they aren't from the US, Canada or Germany, so they would be redirected to blackhatworld.

    If you've got any questions, let me know.

    EDIT: If you'd like to see how to NOT redirect the user at all, but still show different content on the page based on the users geographic location, let me know. What I mean by that is, the visitor lands at: http://yoursite.com/abovescript.php and if they are from the US, instead of being redirected to a different URL, they stay at that exact URL but are shown a US flag. If they are from Germany, they would stay at that exact URL but would be shown a German flag, and so on and so forth.

    It is a lot less conspicuous than doing a full redirect, and helps to stop the visitor from realizing they are being shown country targeted content.
     
    • Thanks Thanks x 2
    Last edited: May 5, 2011
  10. Rasputin78

    Rasputin78 Regular Member

    Joined:
    Sep 14, 2010
    Messages:
    376
    Likes Received:
    82
    Gender:
    Male
    Location:
    Ether
    Yes the tut u wrote is an absolute breeze its perfectly clear and easy to follow.

    And WOW can u really do that? Show same URL but diff content? Man that would be ....I dont have the words to thank u !!! Been looking for something like that for months and everyone i asked said it couldn't be done.

    Please post that when ever u get the time , will test this right away

    And Thanks again man this is really great info :)


    EDIT: Fully setup and it works perfectly :D , Waiting for the other method u mentioned and THANKS Again :)
     
    Last edited: May 5, 2011
  11. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Happy to hear everything is working out for you, it's nice to see the stuff I write is actually read by somebody, haha.

    Anyway, here is the code to show different content to different people, but without having to redirect to a different URL. I'm sure there are other ways, and probably cleaner code, but this will work. This WON'T be the solution you're looking for, if the content you're looking to display is something like, for instance, a CPA lander or anything else where a cookie from an outside site would need to be set.

    You'll need to create a page on your domain, that is blank, and has the following code on it:

    Code:
    <?php
    
    //connect to db
    $con = mysql_connect('localhost', 'YOUR DATABASE USERNAME HERE', 'YOUR DATABASE PASSWORD HERE') or die(mysql_error());
     
     
    mysql_select_db('YOUR DATABASE NAME HERE', $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);
                         
    $CountryCode = $ipData['country_code'];
    
    
    
    if ([COLOR="Cyan"]$CountryCode == 'US'[/COLOR]){$UScontent = file_get_contents('http://freebiefindssite.info/[COLOR="red"]usflag.htm[/COLOR]'); echo $UScontent;}
    else if ([COLOR="Cyan"]$CountryCode == 'DE'[/COLOR]){$DEcontent = file_get_contents('http://freebiefindssite.info/[COLOR="red"]deflag.htm[/COLOR]'); echo $DEcontent;}
    else if ([COLOR="Cyan"]$CountryCode == 'CA'[/COLOR]){$CAcontent = file_get_contents('http://freebiefindssite.info/[COLOR="Red"]caflag.htm[/COLOR]'); echo $CAcontent;}
    else {$undefinedcontent = file_get_contents('[COLOR="red"]http://www.blackhatworld.com[/COLOR]'); echo $undefinedcontent;}
    ?>
    ^^Scroll all the way to the right so you see all the different color text^^



    Fully functional page with the exact code above, is located here: http://freebiefindssite.info/noredirect.php

    First, visit the page, and then visit the page again using unblockbypass.com (which is a proxy site located in Germany). You'll see that the page is completely different, but you haven't been redirected. You can even view the source code, and you'll see the source code has changed completely.

    To accomplish this, you'll need to make each country specific lander, and upload each one to your site (or anywhere onto the web).

    So you'll have http://yoursite.com/uslander.html, http://yoursite.com/calander.html, etc, etc,etc.

    Then, simply put those landers into the code above where I've highlighted (I think you should be able to get it, as you've gotten this far and are probably starting to understand the code).

    Once again, you'll see that if they are visiting from a country that you haven't defined a specific lander for them to see, they end up at seeing blackhatworld.

    If you need any help with the above, let me know.

    EDIT: I also suggest you have a look at this thread (it's a pretty short thread, not even longer than 1 page), and read the comments I & others have on it, as it is very likely going to have an impact on what you're doing:
    Code:
    http://www.blackhatworld.com/blackhat-seo/white-hat-seo/301102-how-display-different-landing-page.html
     
    • Thanks Thanks x 2
    Last edited: May 5, 2011
  12. ehinoze

    ehinoze Power Member

    Joined:
    Feb 1, 2011
    Messages:
    674
    Likes Received:
    108
    Occupation:
    Internet marketing
    Location:
    London
    I should this imediately on my site
     
  13. Autumn

    Autumn Elite Member

    Joined:
    Nov 18, 2010
    Messages:
    2,197
    Likes Received:
    3,041
    Occupation:
    I figure out ways to make money online and then au
    Location:
    Spamville
    Nice one crazy. I do something very similar using the maxmind database but I use text files for portability. It's a great method for CPA stuff where you need to split your different country traffic off to different offers.
     
    • Thanks Thanks x 1
  14. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Yes, it definitely is. You can even get really sneaky, and use a combination of the available data you have at your disposal and start showing things like Google Maps (which you can display using the coordinates given to you in latitude & longitude), say that all users near "X" town (where "X" is their town or one close to it) have been selected for the given "survey" & so on and so forth.

    Displaying content based on location isn't as common as us IM'ers think it is. I sometimes like to "peek over the shoulder" of my Fiance as she uses the internet sometimes (she is hardly ever in front of a computer), and you'd be amazed at what stuff she falls for.

    Just the other day she was tempted to click on a banner that was displaying a town near where I live, before I stopped her of course.
     
    • Thanks Thanks x 3
  15. newyorkpride

    newyorkpride Jr. VIP Jr. VIP Premium Member

    Joined:
    Feb 26, 2008
    Messages:
    156
    Likes Received:
    59
    Thanks for all the hard work. I'm just wondering, what's the advantage of using this over something like here. Premade free scripts that you just call. I also think that the iplocation database, the free one, isn't too accurate(good enough for most purposes though).
     
    • Thanks Thanks x 1
  16. crazyflx

    crazyflx Elite Member

    Joined:
    Nov 9, 2009
    Messages:
    1,674
    Likes Received:
    4,825
    Location:
    http://CRAZYFLX.COM
    Home Page:
    Well, I just visited the location you linked to, and it says this:

    Now, if you're not sending too many queries, I doubt there is going to be much enforcement of that. However I'm sure that if you start sending enough requests, they're going to check it out and if you're not linking to 'em, they'll stop sending you data.

    Another reason to self-host a solution, is you have full control over everything. What if maxmind changes something, their server goes down or they stop serving up data?

    A good business model relies on you being in control of as many variables as possible. For something that requires as little effort as the above installation, it only makes sense for it to be one more thing you DO have control over, at least in my opinion.
     
    • Thanks Thanks x 1
  17. ddvv84

    ddvv84 Regular Member

    Joined:
    Mar 2, 2011
    Messages:
    210
    Likes Received:
    178
    Occupation:
    Junior Network Admin P/T, Live Forex Trader
    Location:
    BROOKLYN, New York
    It's dead on, all the way to my zip code. But i am in new york so they probably have all the networks here layed out.

    Right on @crazyflx post above mine.
     
    • Thanks Thanks x 1
  18. britcpa

    britcpa Power Member

    Joined:
    Mar 25, 2010
    Messages:
    509
    Likes Received:
    1,384
    crazyflix: THANKS & REP given for some great information. will come back to this thread as & when im looking to implement the scripts....
     
    • Thanks Thanks x 1
  19. pkalico

    pkalico Junior Member

    Joined:
    Feb 3, 2011
    Messages:
    159
    Likes Received:
    51
    Holy cow, THANK YOU crazyflx, I was just looking for this info, no joke. Thanks and rep given.
     
    • Thanks Thanks x 1
  20. crzyyoungn

    crzyyoungn Registered Member

    Joined:
    Nov 24, 2010
    Messages:
    86
    Likes Received:
    19
    I placed this message in another thread but then realized that you hadn't responded there for days so I thought I'd place it here since I know you're checking back here:

    Crazyflx,

    I've been trying to reach your support for weeks to no avail. I needed help with using the plugin and list that I bought from you in March and have sent multiple emails to customer support, multiple requests through the support docket on your site and have even commented on your site yesterday to get some help and nothing... The last list was awesome but how can you claim to give a discount to people who've bought from you before when they can't even reach you to get the discount links? I'd like the latest discount link (hopefully while its still valid) and better support please...

    I'll also preemptively mention that I can't PM.