Jump to content

Recommended Posts

Does anyone know of some good ways to filter traffic by country?

 

I can only monetize US, UK, and Canada traffic, but must send them to a different thing for each country.

 

All other countries I would like to send somewhere else.

 

Help is much appreciated.

 

Thanks in advance.

 

PM ME or reply here

Link to comment
https://forums.phpfreaks.com/topic/73239-ip-filtering-by-country-help/
Share on other sites

You cant do it by ipaddress, since countries dont have a single block of ip addy's,  for example Australia might start at have the ip starts of 96. 211.  155. and so on

 

IP address typicaly are assigned to companies and such without regard to location so as I said above an australian company lets for example say tnt might have the ip addresses staring with 213. and 215. but a chinese company might have 214.

 

the best way to try and sort langauges would be by doing $_SERVER["HTTP_ACCEPT_LANGUAGE"]

 

Using that you can narrow it down pretty easily

 

for example in just english speaking countries which are declared by au you have

 

en-au (Australia)

en-bz (Belize)

en-ca (Canada)

en-ie (Ireland)

en-jm (Jamaica)

en-nz (New Zealand)

en-ph (Philippines)

en-za (South Africa)

en-tt (Trinidad)

en-gb (United Kingdom)

en-us (United States)

en-zw (Zimbabwe)

http://1337gamerz.recoding.net/IPLocations.csv

Right Click, Save As that file.

 

Then Use this

function ip2loc($ip) {
list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip);
$ipn = $ip4+$ip3*1000+$ip2*1000000+$ip1*1000000000;
$list = file('IPLocations.csv');
foreach($list as $line) {
   $line = str_replace('"','',$line);
   $line = explode(',',$line);
   $lineip = $line[0];
   list($lineip1,$lineip2,$lineip3,$lineip4) = explode('.',$lineip);
   $lineipn = $lineip4+$lineip3*1000+$lineip2*1000000+$lineip1*1000000000;
   $lineip2 = $line[1];
   list($lineip1,$lineip2,$lineip3,$lineip4) = explode('.',$lineip2);
   $lineipn2 = $lineip4+$lineip3*1000+$lineip2*1000000+$lineip1*1000000000;
   if ($ipn >= $lineipn && $ipn <= $lineipn2) {
   		//for the full name:
	//$loc =  $line[5];
	//for the two letter code:
	$loc = $line[4];
      break;
      }
   }
if(empty($loc)) {
$loc = 'default_here';
}
return $loc;
}

$ip = $_SERVER['REMOTE_ADDR'];
echo $loc

 

That should work, unless i messed up somewhere, if it doesn't let me know :)

if your actualy going to go with that method wouldn't it be better to read that file directly, whoever wrote it up would prob keep it updated....

 

Also rather that write one up myself to show you how do the accept langauge I found this script, just give it a try, it's a lot mroe lightweight file so would process a lot quicker

 

<?php
/*
Script Name: Full Operating system language detection
Author: Harald Hope, Website: http://techpatterns.com/
Script Source URI: http://techpatterns.com/downloads/php_language_detection.php
Version 0.3.4
Copyright (C) 29 June 2007

This program is free software; you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt

Coding conventions:
http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3
*/
/****************************************** 
this will return an array composed of a 4 item array for each language the os supports
1. full language abbreviation, like en-ca
2. primary language, like en
3. full language string, like English (Canada)
4. primary language string, like English
*******************************************/

// choice of redirection header or just getting language data
// to call this you only need to use the $feature parameter
function get_languages( $feature, $spare='' )
{
// get the languages
$a_languages = languages();
$index = '';
$complete = '';
$found = false;// set to default value
//prepare user language array
$user_languages = array();

//check to see if language is set
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ) 
{
	//explode languages into array
	$languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	$languages = explode( ",", $languages );

	foreach ( $languages as $language_list )
	{
		// pull out the language, place languages into array of full and primary
		// string structure: 
		$temp_array = array(); 
		// slice out the part before ; on first step, the part before - on second, place into array
		$temp_array[0] = substr( $language_list, 0, strcspn( $language_list, ';' ) );//full language
		$temp_array[1] = substr( $language_list, 0, 2 );// cut out primary language
		//place this array into main $user_languages language array
		$user_languages[] = $temp_array;
	}

	//start going through each one
	for ( $i = 0; $i < count( $user_languages ); $i++ )
	{
		foreach ( $a_languages as $index => $complete ) 
		{
			if ( $index == $user_languages[$i][0] )
			{
				// complete language, like english (canada) 
				$user_languages[$i][2] = $complete;
				// extract working language, like english
				$user_languages[$i][3] = substr( $complete, 0, strcspn( $complete, ' (' ) );
			}
		}
	}
}
else// if no languages found
{
	$user_languages[0] = array( '','','','' ); //return blank array.
}

// return parameters
if ( $feature == 'data' )
{
	return $user_languages;
}

// this is just a sample, replace target language and file names with your own.
elseif ( $feature == 'header' )
{
	switch ( $user_languages[0][1] )// get default primary language, the first one in array that is
	{
		case 'en':
			$location = 'english.php';
			$found = true;
			break;
		case 'sp':
			$location = 'spanish.php';
			$found = true;
			break;
		default:
			break;
	}
	if ( $found )
	{
		header("Location: $location"); 
	}
	else// make sure you have a default page to send them to
	{
		header("Location: default.php"); 
	}
}
}

function languages()
{
// pack abbreviation/language array
// important note: you must have the default language as the last item in each major language, after all the
// en-ca type entries, so en would be last in that case
$a_languages = array(
'af' => 'Afrikaans',
'sq' => 'Albanian',
'ar-dz' => 'Arabic (Algeria)',
'ar-bh' => 'Arabic (Bahrain)',
'ar-eg' => 'Arabic (Egypt)',
'ar-iq' => 'Arabic (Iraq)',
'ar-jo' => 'Arabic (Jordan)',
'ar-kw' => 'Arabic (Kuwait)',
'ar-lb' => 'Arabic (Lebanon)',
'ar-ly' => 'Arabic (libya)',
'ar-ma' => 'Arabic (Morocco)',
'ar-om' => 'Arabic (Oman)',
'ar-qa' => 'Arabic (Qatar)',
'ar-sa' => 'Arabic (Saudi Arabia)',
'ar-sy' => 'Arabic (Syria)',
'ar-tn' => 'Arabic (Tunisia)',
'ar-ae' => 'Arabic (U.A.E.)',
'ar-ye' => 'Arabic (Yemen)',
'ar' => 'Arabic',
'hy' => 'Armenian',
'as' => 'Assamese',
'az' => 'Azeri',
'eu' => 'Basque',
'be' => 'Belarusian',
'bn' => 'Bengali',
'bg' => 'Bulgarian',
'ca' => 'Catalan',
'zh-cn' => 'Chinese (China)',
'zh-hk' => 'Chinese (Hong Kong SAR)',
'zh-mo' => 'Chinese (Macau SAR)',
'zh-sg' => 'Chinese (Singapore)',
'zh-tw' => 'Chinese (Taiwan)',
'zh' => 'Chinese',
'hr' => 'Croatian',
'cs' => 'Czech',
'da' => 'Danish',
'div' => 'Divehi',
'nl-be' => 'Dutch (Belgium)',
'nl' => 'Dutch (Netherlands)',
'en-au' => 'English (Australia)',
'en-bz' => 'English (Belize)',
'en-ca' => 'English (Canada)',
'en-ie' => 'English (Ireland)',
'en-jm' => 'English (Jamaica)',
'en-nz' => 'English (New Zealand)',
'en-ph' => 'English (Philippines)',
'en-za' => 'English (South Africa)',
'en-tt' => 'English (Trinidad)',
'en-gb' => 'English (United Kingdom)',
'en-us' => 'English (United States)',
'en-zw' => 'English (Zimbabwe)',
'en' => 'English',
'us' => 'English (United States)',
'et' => 'Estonian',
'fo' => 'Faeroese',
'fa' => 'Farsi',
'fi' => 'Finnish',
'fr-be' => 'French (Belgium)',
'fr-ca' => 'French (Canada)',
'fr-lu' => 'French (Luxembourg)',
'fr-mc' => 'French (Monaco)',
'fr-ch' => 'French (Switzerland)',
'fr' => 'French (France)',
'mk' => 'FYRO Macedonian',
'gd' => 'Gaelic',
'ka' => 'Georgian',
'de-at' => 'German (Austria)',
'de-li' => 'German (Liechtenstein)',
'de-lu' => 'German (lexumbourg)',
'de-ch' => 'German (Switzerland)',
'de' => 'German (Germany)',
'el' => 'Greek',
'gu' => 'Gujarati',
'he' => 'Hebrew',
'hi' => 'Hindi',
'hu' => 'Hungarian',
'is' => 'Icelandic',
'id' => 'Indonesian',
'it-ch' => 'Italian (Switzerland)',
'it' => 'Italian (Italy)',
'ja' => 'Japanese',
'kn' => 'Kannada',
'kk' => 'Kazakh',
'kok' => 'Konkani',
'ko' => 'Korean',
'kz' => 'Kyrgyz',
'lv' => 'Latvian',
'lt' => 'Lithuanian',
'ms' => 'Malay',
'ml' => 'Malayalam',
'mt' => 'Maltese',
'mr' => 'Marathi',
'mn' => 'Mongolian (Cyrillic)',
'ne' => 'Nepali (India)',
'nb-no' => 'Norwegian (Bokmal)',
'nn-no' => 'Norwegian (Nynorsk)',
'no' => 'Norwegian (Bokmal)',
'or' => 'Oriya',
'pl' => 'Polish',
'pt-br' => 'Portuguese (Brazil)',
'pt' => 'Portuguese (Portugal)',
'pa' => 'Punjabi',
'rm' => 'Rhaeto-Romanic',
'ro-md' => 'Romanian (Moldova)',
'ro' => 'Romanian',
'ru-md' => 'Russian (Moldova)',
'ru' => 'Russian',
'sa' => 'Sanskrit',
'sr' => 'Serbian',
'sk' => 'Slovak',
'ls' => 'Slovenian',
'sb' => 'Sorbian',
'es-ar' => 'Spanish (Argentina)',
'es-bo' => 'Spanish (Bolivia)',
'es-cl' => 'Spanish (Chile)',
'es-co' => 'Spanish (Colombia)',
'es-cr' => 'Spanish (Costa Rica)',
'es-do' => 'Spanish (Dominican Republic)',
'es-ec' => 'Spanish (Ecuador)',
'es-sv' => 'Spanish (El Salvador)',
'es-gt' => 'Spanish (Guatemala)',
'es-hn' => 'Spanish (Honduras)',
'es-mx' => 'Spanish (Mexico)',
'es-ni' => 'Spanish (Nicaragua)',
'es-pa' => 'Spanish (Panama)',
'es-py' => 'Spanish (Paraguay)',
'es-pe' => 'Spanish (Peru)',
'es-pr' => 'Spanish (Puerto Rico)',
'es-us' => 'Spanish (United States)',
'es-uy' => 'Spanish (Uruguay)',
'es-ve' => 'Spanish (Venezuela)',
'es' => 'Spanish (Traditional Sort)',
'sx' => 'Sutu',
'sw' => 'Swahili',
'sv-fi' => 'Swedish (Finland)',
'sv' => 'Swedish',
'syr' => 'Syriac',
'ta' => 'Tamil',
'tt' => 'Tatar',
'te' => 'Telugu',
'th' => 'Thai',
'ts' => 'Tsonga',
'tn' => 'Tswana',
'tr' => 'Turkish',
'uk' => 'Ukrainian',
'ur' => 'Urdu',
'uz' => 'Uzbek',
'vi' => 'Vietnamese',
'xh' => 'Xhosa',
'yi' => 'Yiddish',
'zu' => 'Zulu' );

return $a_languages;
}
?>

 

you know what? hell with it, this is all overkill

 

all you need to do is this

 

<?php
$location = $_SERVER["HTTP_ACCEPT_LANGUAGE"]; //collects the users location

$activelocation = array('en-au','en-ca,'en-gb','en-us'); //regions you want to do soemthing with

if (in_array($location, $activelcoation))
{
header("location: http://somesite.com;");
exit();
}
else
{
header("location: http://someothersite.com;");
exit();
}
?>

 

thats all you need full stop, that'll do exactly what your asking, no huge scripts needed

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.