Looking for a geoIP "screening" script

marxistseo

New member
Apr 2, 2008
63
0
0
Hey,

I need a script which sends everyone from UK and US to a certain link, and all the others to a different one.

I tried looking into geoIP scripts but they mostly just give you an output of the user's country... How do I redirect everyone from a country of my choice?

Thanks
 


Using MaxMinds free GeoIP

$source_ip = $_SERVER['REMOTE_ADDR'];
include("geoip.inc");
// read GeoIP database
$handle = geoip_open("GeoIP.dat", GEOIP_STANDARD);
// map IP to country
$country_code = geoip_country_code_by_addr($handle, $source_ip);
// close database handler
geoip_close($handle);

if( $country_code == "GB")
{
header ("location: http://myurl.com");
}
elseif( $country_code == "US")
{
header ("location: http://myotherurl.com");
}
else
header ("location: http://defaulturl.com");
 
Using MaxMinds free GeoIP

Using GeoPlugin (same script as yours but with a method of grabbing the country that doesn't require a local file)

Code:
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']))
$target = "http://www.default.com";
switch($geo['geoplugin_countryCode'])
{
   case "GB": $target = "http://www.uk.com"; break;
   case "US": $target = "http://www.us.com"; break;
   case "CA": $target = "http://www.ca.com";
}

header("location: ".$target);
exit();
 
Thanks

+rep

Using GeoPlugin (same script as yours but with a method of grabbing the country that doesn't require a local file)

Code:
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']))
$target = "http://www.default.com";
switch($geo['geoplugin_countryCode'])
{
   case "GB": $target = "http://www.uk.com"; break;
   case "US": $target = "http://www.us.com"; break;
   case "CA": $target = "http://www.ca.com";
}

header("location: ".$target);
exit();
 
I am using the code from Monetizing International Traffic | Brandon Adcock dot com

Code:
[INDENT]<?php
// ccr.php - country code redirect
require_once(’/geo/geoplugin.class.php’);
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;
 switch($country_code) {
case ‘US’:
header(’Location: http://www.yourdomain.com/usoffer.php’);
exit;
case ‘CA’:
header(’Location: http://www.yourdomain.com/caoffer.php’);
exit;
case ‘GB’:
header(’Location: http://www.yourdomain.com/gboffer.php’);
exit;
case ‘IE’:
header(’Location: http://www.yourdomain.com/irelandoffer.php’);
exit;
case ‘AU’:
header(’Location: http://www.yourdomain.com/australiaoffer.php’);
exit;
case ‘NZ’:
header(’Location: http://www.yourdomain.com/newzealandoffer.php’);
exit;
default: // exceptions
header(’Location: http://www.yourdomain.com/allelseoffer.php’);
exit;
}
 ?>

[/INDENT]I have copied and extracted the .php files to a dir /geo

when i load my ccr.php all I get is a blank screen, Any ideas?
 
Have you just copy&pasted that code from somewhere? Sometimes apostrophes get fucked up with c&p.

Try this:

Code:
    <?php
    // ccr.php - country code redirect
    require_once('/geo/geoplugin.class.php');
    $geoplugin = new geoPlugin();
    $geoplugin->locate();
    $country_code = $geoplugin->countryCode;
     switch($country_code) {
    case 'US':
    header('Location: http://www.yourdomain.com/usoffer.php');
    exit;
    case 'CA':
    header('Location: http://www.yourdomain.com/caoffer.php');
    exit;
    case 'GB':
    header('Location: http://www.yourdomain.com/gboffer.php');
    exit;
    case 'IE':
    header('Location: http://www.yourdomain.com/irelandoffer.php');
    exit;
    case 'AU':
    header('Location: http://www.yourdomain.com/australiaoffer.php');
    exit;
    case 'NZ':
    header('Location: http://www.yourdomain.com/newzealandoffer.php');
    exit;
    default: // exceptions
    header('Location: http://www.yourdomain.com/allelseoffer.php');
    exit;
    }
     ?>

And remove all white spaces before <?php and after ?>

I am using the code from Monetizing International Traffic | Brandon Adcock dot com

Code:
[INDENT]<?php
// ccr.php - country code redirect
require_once(’/geo/geoplugin.class.php’);
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;
 switch($country_code) {
case ‘US’:
header(’Location: http://www.yourdomain.com/usoffer.php’);
exit;
case ‘CA’:
header(’Location: http://www.yourdomain.com/caoffer.php’);
exit;
case ‘GB’:
header(’Location: http://www.yourdomain.com/gboffer.php’);
exit;
case ‘IE’:
header(’Location: http://www.yourdomain.com/irelandoffer.php’);
exit;
case ‘AU’:
header(’Location: http://www.yourdomain.com/australiaoffer.php’);
exit;
case ‘NZ’:
header(’Location: http://www.yourdomain.com/newzealandoffer.php’);
exit;
default: // exceptions
header(’Location: http://www.yourdomain.com/allelseoffer.php’);
exit;
}
 ?>
[/INDENT]
I have copied and extracted the .php files to a dir /geo

when i load my ccr.php all I get is a blank screen, Any ideas?
 
I am using the code from Monetizing International Traffic | Brandon Adcock dot com

Code:
[INDENT]<?php
// ccr.php - country code redirect
require_once(’/geo/geoplugin.class.php’);
$geoplugin = new geoPlugin();
$geoplugin->locate();
$country_code = $geoplugin->countryCode;
 switch($country_code) {
case ‘US’:
header(’Location: http://www.yourdomain.com/usoffer.php’);
exit;
case ‘CA’:
header(’Location: http://www.yourdomain.com/caoffer.php’);
exit;
case ‘GB’:
header(’Location: http://www.yourdomain.com/gboffer.php’);
exit;
case ‘IE’:
header(’Location: http://www.yourdomain.com/irelandoffer.php’);
exit;
case ‘AU’:
header(’Location: http://www.yourdomain.com/australiaoffer.php’);
exit;
case ‘NZ’:
header(’Location: http://www.yourdomain.com/newzealandoffer.php’);
exit;
default: // exceptions
header(’Location: http://www.yourdomain.com/allelseoffer.php’);
exit;
}
 ?>

[/INDENT]I have copied and extracted the .php files to a dir /geo

when i load my ccr.php all I get is a blank screen, Any ideas?

... you could just replace everything above the switch() with what I posted above (Same site, same function, just you're using the class for whatever reason).
 
All of those will "work" fine, but relative to a local database they're ungodly slow. If the speed of your redirects matter, it is worth the time to setup a local maxmind DB.
 
+1 for irwin

I've noticed that my local maxmind DB (the free one) displays my town as being the next one over. It's much faster than the javascript version, which hits the town exactly. The javascript version was hanging up my LPs so I am just using the local one now. But I think for the purpose of redirecting you should definitely just use the local DB, since you don't need to pin them to their exact town. you want to rely on external servers as much as possible.
 
This setup is specific to a ZenSix VPS, but should work on any CentOS/Redhat system.

First install Net_GeoIP, the PHP5 pear port.


Then download the GeoIP.dat file and extract it to your public_html folder:


Finally, use the code below to block countries, etc...

<?php

require_once('Net/GeoIP.php');

$redirect_page = "/some-redirect-page-here.html";

$file = dirname(__FILE__)."/GeoIP.dat";

$geoip = Net_GeoIP::getInstance($file);
try {
$country = strtolower($geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']));
} catch (Exception $e) {
$country = '';
}

switch($country) {
case 'cn':
case 'af':
case 'eg':

header("Location: $redirect_page");

}
?>
 
1: Download the ip database from here. It's a PHP database.
2: Upload the folder to your root directory
3: copy the code below and save it as a .php file
4: follow the instructions to add your affiliate links
5: upload it to any folder. it will find the database on its own.

You can also include the php file on the top of your index page and it will let all US visitors stay on the page and send all non-us visitors to landing pages for their own countries or to a catchall landing page.

<?

// Add a new definition for each new country you need a redirect for.
// Copy the code below and change "COUNTRYCODE" to the country you want.
// All country codes can be found in countrycodes.txt.
// Paste the code on the next row below all other definitions without the "//".
// define( COUNTRYCODE_VISITORS, "http://www.ANY-URL-YOU-WANT.com" );
// After you have added a new definition, scroll down to the bottom and add a new case.
// You can remove all rows starting with "//" if you want.

// US = United States
define( US_VISITORS, "http://www.yoursite.com/us" );
// GB = Great Britain
define( GB_VISITORS, "http://www.yoursite.com/gb" );
// CA = Canada
define( CA_VISITORS, "http://www.affiliate-link-for-canadians-etc.com" );

// Anyone who is not defined will be sent to your catchall URL.
define( CATCH_ALL_VISITORS, "http://www.yoursite.com/catchall" );

if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
$ip = $_SERVER['REMOTE_ADDR'];

function iptocountry($ip){
$country_code = "";
$arr = explode( ".", $ip );
if( is_file( $_SERVER["DOCUMENT_ROOT"] . "/ip_files/" . $arr[0] . ".php" ) ) {
include( $_SERVER["DOCUMENT_ROOT"] . "/ip_files/" . $arr[0] . ".php" );
$code = $arr[0] *16777216 + $arr[1] * 65536 + $arr[2] * 256 + $arr[3];

foreach( $ranges as $key=>$value ) {
if( $key <= $code ){
if( $value[0] >= $code ) {
$country_code = $value[1];
break;
}
}
}
}
return $country_code;
}

$country_code = iptocountry( $ip );

// Add a new case for each country. Copy the code below and change COUNTRYCODE to the country you want.

// case "COUNTRYCODE":
// header( "Location: ".COUNTRYCODE_VISITORS );
// exit();

// There is no specific order needed, so just add it between
// "switch( $country_code )" and the closing tag "}".
// It's pretty easy. Just follow the pattern below.
// You can't have a case without having a definition though.
// Then the visitor from that country will get an error.

switch( $country_code ) {

case "US":
header( "Location: ".US_VISITORS );
exit();
break;
case "CA":
header( "Location: ".CA_VISITORS );
exit();
break;
case "GB":
header( "Location: ".GB_VISITORS );
exit();
break;

}

// The following code is sending all visitors that has no case and no
// definition to your catchall URL.
// IMPORTANT! If you don't have a case for US traffic they will stay on the landing page.
// You can put HTML below this script, then people from the US will see that page instead
// of being redirected.
// You can change "US" to any other country and it will work the same way.

if( $country_code != "US" ) {
header( "Location: ".CATCH_ALL_VISITORS );
exit();
}
?>
 
My way of doing it.

1st. Download geoip databases

Then


Code:
function getcountry($getCountryRatherThanCode=false)
{
    static $country; //Store country and country code in static variable because it won't change for the whole session
    static $countryCode;
    
    
    
reportDebugMessage("Country and Country Code at start " . $country."|". $countryCode . "|Line: ". __LINE__ . "File: " .__FILE__." Function: ". __FUNCTION__ . "<br/>" ); 
    
    
    if ($country=="" || $countryCode=="")
    {
    }
    else
    {
        if ($getCountryRatherThanCode==false)
        {
            return $countryCode;
        }
        else
        {
            return $country;
        }
    }

    if (isset($_GET['ascountry']))
    {
        $countryCode=$_GET['ascountry'];
        $country='Set by Others';
    }    
    
    if ($country=="" || $countryCode=="")
    {
        if (!realVisitors())
        {
            $countryCode=US;
            $country="US US an";            
            echo "//Called Once:getcountry" . $countryCode . $country;
        }
    }
    
reportDebugMessage("Country and Country Code when we know it's real visitors " . $country."|". $countryCode . "|Line: ". __LINE__ . "File: " .__FILE__." Function: ". __FUNCTION__ . "<br/>" ); 
    
    if ($country=="" || $countryCode=="")
    {

        $buffer=getCountryFromDatabase($_SERVER["REMOTE_ADDR"]);

        $buffer=explode("|",$buffer);

        $countryCode=$buffer[0];
        $country=$buffer[1];        
    }

    
 reportDebugMessage("Country and Country Code after what should have changed " . $country."|". $countryCode . "|Line: ". __LINE__ . "File: " .__FILE__." Function: ". __FUNCTION__ . "<br/>" ); 

    /*if ($country=="" || $countryCode=="")
    {
        
        if (file_exists("geoip/getcountryfunction.php"))
        {
            //echo "//I exist from getcountry.php";
            require_once "geoip/getcountryfunction.php";
            $buffer = IPCode($_SERVER["REMOTE_ADDR"],"geoip/");
        }
        else
        {
            $buffer="http://69.93.191.226/~refinan1/geoip/getcountrycomplete.php?ip=" . $_SERVER["REMOTE_ADDR"];
            $buffer=usecurl($buffer);
            //echo "//Used Curl to Get country";            
        }
        
        $buffer=explode("|",$buffer);
        $countryCode=$buffer[0];
        $country=$buffer[1];            
    }*/

    if ($country=="" || $countryCode=="")
    {
        $countryCode='FK';
        $country='FUCKUP';            
    }

 reportDebugMessage("Measure Time: " . measuretime1() . "|Line: ". __LINE__ . "File: " .__FILE__." Function: ". __FUNCTION__ . "<br/>" ); 
    trackCountry();    
reportDebugMessage("Measure Time: " . measuretime1() . "|Line: ". __LINE__ . "File: " .__FILE__." Function: ". __FUNCTION__ . "<br/>" ); 
    //echo $countryCode."|".$country;
    return getcountry($getCountryRatherThanCode);

}
 
Using GeoPlugin (same script as yours but with a method of grabbing the country that doesn't require a local file)

Code:
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']))
$target = "http://www.default.com";
switch($geo['geoplugin_countryCode'])
{
   case "GB": $target = "http://www.uk.com"; break;
   case "US": $target = "http://www.us.com"; break;
   case "CA": $target = "http://www.ca.com";
}

header("location: ".$target);
exit();


If you do this, how many requests you can make per day? Remember you're using your server IP to connect.

What am I missing.