Help Agnivo buy himself a Macbook Pro laptop on birthday. Make his dream come true! [ Click here for more info ]

Agnivo Roy RSS Feed

Block Non-converting, Spam Traffic by Country : GeoIP PECL PHP Geo-Targeting

15 06 2007

Welcome! Please [ subscribe to my RSS Feed ] OR get [ updates by email ].
agnivo.com is mobile device compatible! Thanks, visit us often :-)

Block Traffic by CountryI’m sure webmasters have faced situations where they need a fast, affordable and working solution to block less converting or spam traffic to their websites or pages from certain countries, but either they had to implement slow, inefficient methods or costly professional solutions for their Geo targeting needs.

One of the most popular, free, open-source and hot web programming languages today is PHP. Today, I’m going to share a little trick which efficiently solves the geo-targeting issue; or making your website appear only to a certain demographic you’re targeting and block non-converting or spammy traffic from other countries. Perhaps you know that the IP of the visitors can be translated into country and other info; we are making use of the same in this tutorial.

Geo-targeting, GeoIP or IP to country implementation is a great solution for the following situations, but not limited to them :

  • You have a site or web page targeted to visitors from a certain country; say : USA.
  • You are getting spammed and fraud orders from a certain country; say : China.
  • You don’t want people from certain country or area to access your site.
  • You want to block countries which have less conversion for publisher programs like Google Adsense or YPN.
  • Your web proxy is being abused and less conversion from ads from countries like China and the Middle East; using up system resources and bandwidth.

Now, let’s come back to the tutorial and setup GeoIP based web traffic filtering.

Requirements and softwares used :

  • Preferably Linux OS on server. (tutorial based on Linux)
  • Root access to the server or VPS.
  • PHP 4.3 or higher installed .
  • GeoIP PECL module for PHP.
  • GeoIP C API from Maxmind. (We are using the free GeoIP DB, not 100% accurate)
  • Moderate Linux Server Administration skills.

The Steps :
1. Login as root to your server through SSH.

2. Download and install latest GeoIP C API.
(remove the - in w-get, there’s some posting restrictions here.)
#w-get http://www.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
Note the the GeoIP version downloaded.
#tar -zxvf GeoIP-[version number].tar.gz
#cd GeoIP-[version number]
#./configure
#make
#make install

The GeoIP free C API should be installed now.

3. Install GeoIP PECL module for PHP.
#pecl install geoip-0.2.0
v0.2.0 is the the current version at the time of writing this tutorial.
Check PECL GeoIP for more info and use the latest version in the above statement.

Note down where the “geoip.so” file is saved when the installer terminates.

Now, let’s add it to php.ini and restart the web server to take effect.
#whereis php.ini
#nano /path/to/php.ini

Add the following line, preferably in the dynamic extensions section :
extension = "/path/to/geoip.so"
Save and exit.

Restart the web server, assuming Apache on CentOS:
#/sbin/service httpd restart

To verify if GeoIP extension is running, make info.php containing phpinfo() function and upload to your domain, open it with a browser and search the page for “geoip”.

4. Implement geo-targeting in your website and web pages as necessary.

Here’s an example php snippet if added to the top of a php file blocks any traffic except USA, UK, Canada and Australia : ( replace [ with < below )
[?php
$country = geoip_country_code_by_name($_SERVER["REMOTE_ADDR"]);
if (!(($country == "US") or ($country == "CA") or ($country == "GB") or ($country == "AU"))) { die("Service Unavailable!"); }
?]

I hope the above tutorial would help you to address issues related to geo-targeting and banning unwanted traffic from non converting, fraud and spamming countries.

If anything is unclear, you face some problems or have some questions, feel free to comment below and leave your feedback. I’d be grateful if you socially bookmark this article using the button below so that more people can easily find this tutorial.

Please Rate This Article : 1 Star2 Stars3 Stars4 Stars5 Stars [ 3 votes, average: 4 out of 5 ]
Loading ... Loading ...

Disclosure Policy rss Comments RSS   trackback Trackback
Posted on June 15, 2007 and Last Updated on June 16, 2007 in Webmasters.
Tags : , , , , , , , , ,

Other Interesting Posts

One response to “Block Non-converting, Spam Traffic by Country : GeoIP PECL PHP Geo-Targeting”

8 07 2007
Ip2Country (02:35:45) :

Check the free open-source ip2country php script and ditch GeoIP. Have your own fresh database and update as often as you wish.

Cheers to open source!

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>