Skip to content


GeoIP Scripts PHP and JavaScript

Last week I was reading Danger Brown’s “Easy geo/ip script to be sneaky with“, and oddly enough just days before that I was working on a server side solution to do the same thing. His example is using maxmind’s geoip.js script to determine where your referring IP address is coming from. Here is the script to do this client side using javascript.

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">document.write(geoip_city());</script>
<script language="JavaScript">document.write(geoip_region());</script>

MaxMind creates IP geolocation services and offers this server for free or at a cost, depending on your needs. I wanted to be able to add some conditionals in my example, and have this be available for bots and browsers that do not support javascript. The solution is to grab the geoip information serverside, and then generate the output in HTML using PHP.

To do this you will need to make sure that your version has the pecl-geoip extension available. I’m using dev-php5/pecl-geoip. After this extension is built and is available to your PHP installation, you can simple call this geoip_record_by_name() function and pass a valid IP address.

For example, this is looking up a Google IP:

$record = geoip_record_by_name(’74.125.67.100′);
var_dump($record);

$record is a array that contains some useful information about that IP:

array
‘country_code’ => string ‘US’ (length=2)
‘region’ => string ‘CA’ (length=2)
‘city’ => string ‘Mountain View’ (length=13)
‘postal_code’ => string ’94043′ (length=5)
‘latitude’ => float 37.4192008972
‘longitude’ => float -122.057403564
‘dma_code’ => int 807
‘area_code’ => int 650

I had some problems with a missing GeoLiteCity.dat file that was missing. LiteCity is free, and any additional dats will cost money. Luckily, MaxMind had a download here: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

If you need function definitions, you can view them here. I was glad that I found this forum entry, pecl documentation and examples usually suck pretty bad:
http://forum.maxmind.com/viewtopic.php?p=44

Incoming search terms:

  • php webinar script
  • geoip js
  • geoip javascript
  • geoip js download
  • geo ip javascript
  • javascript geoip
  • j maxmind/app/geoip js
  • how to use geoip javascript
  • geoip js vs php
  • maxmind javascript web service forum


Related posts:

  1. Monitor your sites – Watch your logs
  2. Capture search data on your site
  3. Problems fetching CJ Product Feeds with PHP’s FTP functionality
  4. FTP’ing files from CJ’s FTP server to your host
  5. PHP for Affiliate Marketers

Posted in Affiliate Marketing, Programming.

Tagged with , , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. yo says

    great, there’s any restriction with that javascript?

  2. Jack Fisher says

    thanks for the info
    geoip still long way to go .all depends where the servers are and this causes problems



Some HTML is OK

or, reply to this post via trackback.

CommentLuv badge