GeoIP Tutorial: Forums

In this part of the SiteGround's GeoIP tutorial we will show how to implement the GeoIP functionality in several of the most popular php forum scripts.

GeoIP Implementation in vBulletin

VBulletin is a widely used php forum script. More details about its installation and the functionality can be found in the SiteGround's comprehensive vBulletin tutorial:

http://www.siteground.com/tutorials/vbulletin/index.htm

The GeoIP addon file mip-geoiptracker-1.0.xml can be downloaded from the following location:

http://www.vbulletin.org/forum/showthread.php?p=1532639

It should be installed through your vBulletin admin control panel -> Plugins & Products -> Manage Products -> Add/Import Product.

Next, you should get the geoip.inc and the GeoLite Country Binary Format files. Extract the archive of the last file on your local computer and then upload both the files in the includes folder of your vBulletin script.

In this way the GeoIP solution is integrated in your vBulletin forum.

You can use it by including one of the following code excerpts:

$bbuserinfo[country]

or

$vbulletin->userinfo[country]

The first one should be included in the templates code and the second one is suitable for the php files code.

For example, if you want to visualize the country of your web site visitors in your forum's navigation bar you should edit the navbar template. This can be completed through your vBulletin admin control panel -> Styles & Templates -> Style Manager -> Edit Templates -> Go -> Navigation / Breadcrumb Template -> navbar.

Insert the following code just above the </div> <!-- / nav buttons bar --> lines:

<tr>Country Location: <br />
<if condition="$bbuserinfo[country] == 'US'">Home Country:<br/>
$bbuserinfo[country]
<else />Foreign Country: <br />
$bbuserinfo[country]</if>
</tr>

You can modify the code per your needs.

The result can be seen on the main page of your forum:

Another useful implementation is the restriction of the forum registration based on the country of the visitor. It is a wise solution in the cases when many spammers from a single country or a couple of countries register and enter numerous spam posts in your forums.

The task can be completed by editing the register.php file, which is located in your main vBulletin folder.

Include the following code above the // ############################### start checkdate ############################### line:

#GeoIP checkif ($vbulletin->userinfo[country] == "XX" || $vbulletin->userinfo
[country] == "YY") die("The forum registration is not available for your country");

Replace XX and YY with the country codes of the corresponding countries for which the access should be restricted.

The above solution is good not only for the spam prevention but in the case you want to decrease the server load generated by the forum and make it accessible just for defined countries.

You should make small modifications in the code:

#GeoIP checkif ($vbulletin->userinfo[country] != "XX" || $vbulletin->userinfo[country] != "YY") die(
"The forum registration is not available for your country");

GeoIP Implementation in SMF

SMF is a popular open-source php forum script. Detailed instructions about its installation and management can be found in the SiteGround's SMF tutorial:

http://www.siteground.com/tutorials/smf/

There is not an official GeoIP modification for SMF. However, you can use the corresponding API for your needs.

In order to filter the forum registration based on the country of the visitors you should complete the following modifications:

  • 1. Download the geoip.inc and the GeoLite Country Binary Format files. Extract the last and put them in your SMF root folder.
  • 2. Open the Sources/Register.php file and add the following code below the if (!defined('SMF')) die('Hacking attempt...'); line:
$website_root_path='/home/user/public_html/smf/';
require($website_root_path . 'geoip.inc');
$ip=$_SERVER['REMOTE_ADDR'];$gi=geoip_open($website_root_path .
'GeoIP.dat',GEOIP_STANDARD);
$ccode=geoip_country_code_by_addr($gi, $ip);
if ($ccode=="XX" OR $ccode=="YY"){die("The forum registration is currently not
available for your country.");}geoip_close($gi);

In the above code swap XX and YY with the country code of the countries for which the forum registration should be disabled. Also, as the $website_root_path value enter the absolute path to your SMF root directory.

By following the instructions from the GeoIP Tutorial: Implementation you can modify the above code according to your specific needs.