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

skweekykleen

Newbie
Joined
Jul 8, 2008
Messages
47
Reaction score
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!
 
nice post, all link works. Now I've got ideas in mind for a new target market. Thanks a lot.
 
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:
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?
 
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.
 
Thanks for putting this together.

@rah5013: it just shows you where the user came from it doesn't get you traffic.
 
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.
 
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
 
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

try <?php echo $user_city; // shows the user's city ?> ;)
 
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:
Back
Top