title: php - redirect visitor based on ip address country code
body: <p>I'm trying to use Chandra's answer here: <a href="https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip">Getting visitors country from their IP</a></p>
  
  <p>Here is Chandra's function:</p>
  
  <pre><code>&lt;?php
  
  function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
      $output = NULL;
      if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
          $ip = $_SERVER["REMOTE_ADDR"];
          if ($deep_detect) {
              if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
              if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                  $ip = $_SERVER['HTTP_CLIENT_IP'];
          }
      }
      $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
      $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
      $continents = array(
          "AF" =&gt; "Africa",
          "AN" =&gt; "Antarctica",
          "AS" =&gt; "Asia",
          "EU" =&gt; "Europe",
          "OC" =&gt; "Australia (Oceania)",
          "NA" =&gt; "North America",
          "SA" =&gt; "South America"
      );
      if (filter_var($ip, FILTER_VALIDATE_IP) &amp;&amp; in_array($purpose, $support)) {
          $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
          if (@strlen(trim($ipdat-&gt;geoplugin_countryCode)) == 2) {
              switch ($purpose) {
                  case "location":
                      $output = array(
                          "city"           =&gt; @$ipdat-&gt;geoplugin_city,
                          "state"          =&gt; @$ipdat-&gt;geoplugin_regionName,
                          "country"        =&gt; @$ipdat-&gt;geoplugin_countryName,
                          "country_code"   =&gt; @$ipdat-&gt;geoplugin_countryCode,
                          "continent"      =&gt; @$continents[strtoupper($ipdat-&gt;geoplugin_continentCode)],
                          "continent_code" =&gt; @$ipdat-&gt;geoplugin_continentCode
                      );
                      break;
                  case "address":
                      $address = array($ipdat-&gt;geoplugin_countryName);
                      if (@strlen($ipdat-&gt;geoplugin_regionName) &gt;= 1)
                          $address[] = $ipdat-&gt;geoplugin_regionName;
                      if (@strlen($ipdat-&gt;geoplugin_city) &gt;= 1)
                          $address[] = $ipdat-&gt;geoplugin_city;
                      $output = implode(", ", array_reverse($address));
                      break;
                  case "city":
                      $output = @$ipdat-&gt;geoplugin_city;
                      break;
                  case "state":
                      $output = @$ipdat-&gt;geoplugin_regionName;
                      break;
                  case "region":
                      $output = @$ipdat-&gt;geoplugin_regionName;
                      break;
                  case "country":
                      $output = @$ipdat-&gt;geoplugin_countryName;
                      break;
                  case "countrycode":
                      $output = @$ipdat-&gt;geoplugin_countryCode;
                      break;
              }
          }
      }
      return $output;
  }
  
  ?&gt;
  </code></pre>
  
  <p>Here is one of Chandra's examples on how to use:</p>
  
  <pre><code>&lt;?php
  
  echo ip_info("Visitor", "Country"); // India
  echo ip_info("Visitor", "Country Code"); // IN
  echo ip_info("Visitor", "State"); // Andhra Pradesh
  echo ip_info("Visitor", "City"); // Proddatur
  echo ip_info("Visitor", "Address"); // Proddatur, Andhra Pradesh, India
  
  print_r(ip_info("Visitor", "Location")); // Array ( [city] =&gt; Proddatur [state] =&gt; Andhra Pradesh [country] =&gt; India [country_code] =&gt; IN [continent] =&gt; Asia [continent_code] =&gt; AS )
  
  ?&gt;
  </code></pre>
  
  <p>I can get the example to work just fine, but I want to store that data to recall later and use an if statement to send UK visitors to my .co.uk domain rather than the .com. Here's what I've got at the moment. All of my efforts to do this redirect anyone and everyone to the targetURL; not just people from the UK. I've tried different syntax variations, but I'm a pretty low-level coder.</p>
  
  <pre><code>&lt;?php
  
  $c = ip_info("Visitor", "Country Code");
  if ($c = "GB") {header("Location: targetURL");}
  
  ?&gt; 
  </code></pre>
  
  <p>Any help, advice, suggestions, etcetera will be immensely appreciated.</p>