Need help installing GeoIP2

JimHalpert

Supreme Member
Joined
Jan 16, 2016
Messages
1,345
Reaction score
292
Hi everyone,

I happened to be reading threads of people making geo-rotating scripts using Geolite as per this post (https://www.blackhatworld.com/seo/need-someone-to-code-for-me-php-geo-targetting.41585/#post-410382).

It seems like GeoLite has been changed to GeoIP2 and the installation procedure seems alot more complicated (https://packagist.org/packages/geoip2/geoip2). Could someone provide some guidance on how to install GeoIP2. For example, on the installation page it is stated:

To download Composer, run in the root directory of your project:

curl -sS https://getcomposer.org/installer | php
You should now have the file composer.phar in your project directory.
For example, how does one run it in your project root directory?

More importantly, would the script in the post above still work? I have reproduced it below.

PHP:
<?php

include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);

$country_code = geoip_country_code_by_addr($gi, "$ip");

// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");

geoip_close($gi);

switch($country_code)

    {
        case "US": header("Location: http://Your_American_page"); break;
        case "CA": header("Location: http://Your_Canadian_Page"); break;
        case "UK": header("Location: http://Your_U.K._Page"); break;
        case "AU": header("Location: http://Your_Australian_Page"); break;
        default: header("Location: http://Where_the_rest_of_the_visitors_go");
}
?>

Many thanks.
 
Last edited:
May need more clarification - I'm not sure I understand your question.

The "geoip.inc" file needs to be included. So if you just do
PHP:
include("geoip.inc");

Then it must be in the same folder. You can move it around if you'd like. Since it's an "include file", you generally don't want to put it anywhere that's publicly accessible. So your main page is in the "public_html" (or "www") folder in the root. You can make an "include" folder in the root. Then include it like this:
PHP:
include("../include/geoip.inc");
That's "relative path", you can also do "absolute path" if you want.

Same concept for the .dat file.

Can you clarify what the problem is? Does the code above not work?
 
Back
Top