Hey There!

I have a small t-shirt site that has a European and US store.. I was wondering if it's possible to detect where the user comes from and then redirect them? (perhaps using the ip2cc module)

thanks
/andreas

Comments

koalition’s picture

Think I found my own answer..

.
<?php
$ip = get_remote_ip();
$location = http_get( "http://api.hostip.info/?ip=$ip" );

$contents = get_tag_contents( $location, "Hostip" );
$city = trim( get_tag_contents( $contents, "gml:name" ) );
$country = trim( get_tag_contents( $contents, "countryAbbrev" ) );

if( stristr( $city, "private" ) ) {
  $city = "";
}

if( stristr( $country, "xx" ) ) {
  $country = "US";
}


function get_remote_ip() {
  return $_SERVER['REMOTE_ADDR'];
}

function http_get( $url ) {
  $request = fopen( $url, "rb" );
  $result = "";

  while( !feof( $request ) ) {
   $result .= fread( $request, 8192 );
  }
 
  fclose( $request );
 
  return $result;
}

function get_tag_contents( $xml, $tag ) {
  $result = "";
  $s_tag = "<$tag>";
  $s_offs = strpos( $xml, $s_tag );

  // If we found a starting offset, then look for the end-tag.
  //
  if( $s_offs ) {
   $e_tag = "</$tag>";
   $e_offs = strpos( $xml, $e_tag, $s_offs );

   // If we have both tags, then dig out the contents.
   //
   if( $e_offs ) {
     $result = substr(
       $xml,
       $s_offs + strlen( $s_tag ),
       $e_offs - $s_offs - strlen( $e_tag ) + 1 );
   }
  }

  return $result;
} 
	
?>

and you have a $country var..