Wednesday, May 30, 2012

URL redirection after tracking the visitor's IP address it will automatically land the visitor to the corresponding website in Magento

First crate a file in root folder named: ip.php, and copy/paste following code or download from http://ipinfodb.com/index.php


<?php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = 'efd67dca864cbe8b45fb21b7d19916bf471afd1bfcfe0b3e975d1f238d6c7ed3';

public function __construct(){}

public function __destruct(){}

public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}

public function getError(){
return implode("\n", $this->errors);
}

public function getCountry($host){
return $this->getResult($host, 'ip-country');
}

public function getCity($host){
return $this->getResult($host, 'ip-city');
}

private function getResult($host, $name){
$ip = @gethostbyname($host);

if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');

try{
$response = @new SimpleXMLElement($xml);

foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}

return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}

$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>


Then open index file of magento nad copy/paste following code at the top


$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$url = $protocol.'://'.$_SERVER['SERVER_NAME']; //  More efficient manner
//echo $url;

include('ip.php');
$ipLite = new ip2location_lite;
//$ipAddress = '62.68.127.255';
$ipAddress = $_SERVER['REMOTE_ADDR'];
$locations = $ipLite->getCountry($ipAddress);
$countryCode = strtoupper(trim($locations['countryCode']));
//$countryCode='CA';
//$country_cd = substr($_SERVER['SERVER_NAME'],0,-2);



switch($countryCode){
case 'CA': /* Cananda */
if($url != 'http://www.yourdomain.ca'){
header('location: http://www. yourdomain .ca'.$_SERVER['REQUEST_URI']);
}
break;

case 'UK': /* United States */
if($url != 'http://www. yourdomain .co.uk'){
header('location: http://www. yourdomain .co.uk'.$_SERVER['REQUEST_URI']);
}
break;

case 'DE': /* Germany/Dutch */
if($url != 'http://www. yourdomain .de'){
header('location: http://www. yourdomain .de'.$_SERVER['REQUEST_URI']);
}
break;

case 'SE': /* Sweden */
if($url != 'http://www. yourdomain .se'){
header('location: http://www. yourdomain .se'.$_SERVER['REQUEST_URI']);
}
break;

/* For European Countries Start */

case 'RU': //Russia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'FR': //France
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'UA': //Ukraine
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'ES': //spain
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'SE': //sweden
if($url != 'http://www.handbagbutler.eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'NO': //Norway
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'FI': //Finland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'PL': //Poland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IT': //Italy
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RO': //Romania
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BY': //Belarus
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'KZ': //Kazakhstan
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'GR': //Greece
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BG': //Bulgaria
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IS': //Iceland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'HU': //Hungary
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'PT': //Portugal
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RS': //Serbia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu/'.$_SERVER['REQUEST_URI']);
}
break;

case 'AT': //Austria
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'CZ': //Czech Republic
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'IE': //Republic of Ireland
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'GE': //Georgia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'LT': //Lithuania
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'LV': //Latvia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'RO': //Croatia
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

case 'BA': //Bosnia and Herzegovina
if($url != 'http://www. yourdomain .eu'){
header('location: http://www. yourdomain .eu'.$_SERVER['REQUEST_URI']);
}
break;

/* For European Countries End */

default: /* Default */
if($url != 'http://www. yourdomain .com'){
header('location: http://www. yourdomain .com'.$_SERVER['REQUEST_URI']);
}
break;
}




No comments:

Post a Comment