Question

I am not expert but I am trying to monitor my traffic I was trying to create page with code that detect users Geo and redirect them to new page

But its look like something is missing and I dont know what

I try to use this Geo redirect user just once using php

But I can't find where I get this GeoIP.php

Can someone give me tip what I need to make it work

Thanks Boaz

Was it helpful?

Solution

You should check this out: http://www.php.net/manual/en/function.geoip-continent-code-by-name.php

Simply use

$country = geoip_continent_code_by_name ( $_SERVER['REMOTE_ADDR'] );

$country will return the 2 letter country code. Here is what you should be able to use:

<?php

   //Get the country code of the user, using REMOTE_ADDR is the most reliable way to do this
   $country = geoip_continent_code_by_name ( $_SERVER['REMOTE_ADDR'] );

  //Create a switch case to deal with the country codes
  switch((string)$country) {
    case 'AU':
      $url = "http://www.site.au";
      break;
    case 'CA':
      $url = "http://www.site.ca";
      break;

    //default case means, if you didn't provide a case (country code in your example), do this (otherwise known as a "catch all")
    default:
      $url = "http://defaultsite.com";

      } //End switchcase

  //This if statement says as long as $url is not empty (! means not), update header location (this is the php way of forwarding)
  if ( !empty($url) ){
      header('Location: '.$url);
  } //End if statement

?>

You'll notice I removed the try, catch blocks. This is user preference however, most people do not like it (using a switch case that is why you provide a default case).

That should work 100% for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top