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

using Geo-Targeting with php, a beginner's tutorial

Discussion in 'Black Hat SEO Tools' started by skweekykleen, Apr 2, 2009.

  1. skweekykleen

    skweekykleen Newbie

    Joined:
    Jul 8, 2008
    Messages:
    45
    Likes Received:
    68
    *note: I don't post much, so all my links are still blatant spam attempts, and as such are not permitted. Please replace (HTTP) with the appropriate stuff, and (COM) with the dot and the com required for use.

    Ok, so after speaking with a few people in PM and seeing a few requests here for scripts like this, I've decided to post a simple php script to handle all of your geo-targeting needs.

    First, you're going to need some files for this all to work. If you don't have PHP/Apache/etc installed, you'll have to figure that out and then come back...same applies if you don't know how to set file permissions, copy, use ftp, wipe, type, or use a mouse.

    Go to
    Code:
    (HTTP)geolite.maxmind(COM)/download/geoip/api/php/
    Once you're there, download these files:

    geoip.inc
    geoipcity.inc
    geoipregionvars.php

    Change the files geoip.inc and geoipcity.inc to .php...so you should end up with geoip.php and geoipcity.php when you're done.

    Next, download the GeoLiteCity.dat file from here:
    Code:
    (HTTP)geolite.maxmind(COM)/download/geoip/database/GeoLiteCity.dat.gz
    ok, now create a new file and call it iplocate.php, then place it with all of the files you just downloaded, into a new folder called geo.

    You should now have a folder named 'geo' that contains geoip.php, geoipcity.php, geoipregionvars.php, iplocate.php, and GeoLiteCity.dat

    Now, open iplocate.php (it should be a blank file at the moment) in your favorite php editor, and insert the following:


    Code:
    <?php
    
    //	grab the scripts we're going to need...
    require_once("geoip.php");
    require_once("geoipcity.php");
    require_once("geoipregionvars.php");
    
    //	get user's ip, and look it up in the database...
    $ip_address = trim($_SERVER['REMOTE_ADDR']);
    $gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
    $record = geoip_record_by_addr($gi, $ip_address);
    
    //	assign the most common vars here...
    $user_state	= trim($GEOIP_REGION_NAME[$record->country_code][$record->region]);
    $user_city	= trim($record->city);
    $user_zip	= trim($record->postal_code);
    
    //print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
    //print $record->latitude . "\n";
    //print $record->longitude . "\n";
    //print $record->dma_code . "\n";
    //print $record->area_code . "\n";
    
    geoip_close($gi);
    
    ?>
    
    You may need to change one thing...you might have to put the full path to the GeoLiteCity.dat file in where it currently says geoip_open("GeoLiteCity.dat"

    If so, then you will have to change it to geoip_open("/home/myacct/public_html/geo/GeoLiteCity.dat" or something like that...you should be able to figure that out...

    then save, close, and you're ready...now upload this folder to wherever it is that you need to use it at.

    to use it in your web pages, first of all make sure your page is a .php page, as obviously .html pages won't work unless you've modified your php.ini and if you know how to do that, then you're not reading this. So make sure it's a .php page, then add the following code to it, near the top if possible...

    Code:
    <?php
    require_once("geo/iplocate.php");
    ?>
    
    Now, as long as you've included that little piece of code near the top of your page, you can put this wherever you want to see the city, state, etc in the page:

    Code:
    <?
    php echo $user_state; // shows the user's state 
    ?>
    
    <?
    php echo $user_city; // shows the user's city
    ?>
    
    <?php 
    echo $user_zip; // shows the user's zip code
    ?>
    
    have fun!
     
    • Thanks Thanks x 17
  2. xpressioniz

    xpressioniz Junior Member

    Joined:
    Jun 4, 2008
    Messages:
    121
    Likes Received:
    15
    nice post, all link works. Now I've got ideas in mind for a new target market. Thanks a lot.
     
  3. skweekykleen

    skweekykleen Newbie

    Joined:
    Jul 8, 2008
    Messages:
    45
    Likes Received:
    68
    no problem...glad to help :)
     
  4. lurnme

    lurnme Regular Member

    Joined:
    Jul 16, 2008
    Messages:
    309
    Likes Received:
    93
    Also need to edit the file geoipcity.php.

    Code:
    require_once 'geoip.inc' to require_once 'geoip.php'
    Was just working on this so you saved me a ton of time looking stuff up. Thanks

    Other then that up and running in a few minutes.
     
    Last edited: Apr 11, 2009
  5. CPAgod

    CPAgod Newbie

    Joined:
    Apr 3, 2009
    Messages:
    12
    Likes Received:
    0
    Thanks.
     
  6. undeadlink

    undeadlink Newbie

    Joined:
    Feb 12, 2009
    Messages:
    37
    Likes Received:
    16
    Has anyone had any trouble getting this to work in WP? I think I have everything edited correctly, and it appears to be grabbing the scripts correctly... but when I get to actually displaying the geo targeted info, the page stops loading once it gets to the line with php echo $user_state;

    Any suggestions?
     
  7. harrisunderwork

    harrisunderwork BANNED BANNED

    Joined:
    Jan 20, 2009
    Messages:
    52
    Likes Received:
    4
    An online API to help in this :

    http://iplocationtools.com/
     
    • Thanks Thanks x 1
  8. letusgo

    letusgo Junior Member

    Joined:
    Nov 15, 2008
    Messages:
    199
    Likes Received:
    125
    skweekykleen,harrisunderwork thanks, i just now want to code geo-targetting visitor php and need detailed info.
     
  9. rah5013

    rah5013 Newbie

    Joined:
    Oct 20, 2008
    Messages:
    45
    Likes Received:
    10
    could you tell me what exactly the script do? is it to get traffic by geo targetting or to see where our visitor come from?

    i am new to this php stuff.
     
  10. Syowred

    Syowred BANNED BANNED

    Joined:
    Jun 9, 2008
    Messages:
    53
    Likes Received:
    15
    Thanks for putting this together.

    @rah5013: it just shows you where the user came from it doesn't get you traffic.
     
  11. kraze98nyc

    kraze98nyc Newbie

    Joined:
    Nov 14, 2008
    Messages:
    26
    Likes Received:
    5
    Occupation:
    Internet Marketing - Duh
    Location:
    USA
    Does the state show the abbreviation or full state name?
     
  12. 44blackhatters

    44blackhatters Registered Member

    Joined:
    Jan 21, 2008
    Messages:
    52
    Likes Received:
    3
    Thanks...this is great. I was using another script but found the cities were not very accurate when I tested out a few IP's. Appreciate the help with php. Hopefully I can tweak it to work in Wordpress too.
     
  13. bradman

    bradman Newbie

    Joined:
    Nov 26, 2008
    Messages:
    48
    Likes Received:
    4
    hey guys,

    been trying to get this script working hours now.. im buggin out here.

    i keep getting this error - Parse error: syntax error, unexpected T_ECHO in /homepages/27/d238947933/htdocs/website/index.php on line 43

    i've tried everything and keep getting this error.

    line 43 is the insert code -
    <?
    php echo $user_city; // shows the user's city
    ?>

    ANY IDEAS??!???

    thanks dudes
     
  14. ForallPacific

    ForallPacific Newbie

    Joined:
    Jul 5, 2010
    Messages:
    1
    Likes Received:
    0
    try <?php echo $user_city; // shows the user's city ?> ;)
     
  15. ManToMan

    ManToMan Regular Member

    Joined:
    Feb 27, 2008
    Messages:
    263
    Likes Received:
    289
    Occupation:
    SEO, Design
    Location:
    Green World
    follow exactly but it is blank.

    I place the following code in wordpress after /head before /body
    Code:
    <?php
    require_once("geo/iplocate.php");
    ?>
    
    I place the following code in exe-widget code.

    Code:
    <?
    php echo $user_state; // shows the user's state
    ?>
    
    <?
    php echo $user_city; // shows the user's city
    ?>
    
    <?php
    echo $user_zip; // shows the user's zip code
    ?>
    
    
    Anyone can get it work?
     
    Last edited: Jul 19, 2010