title: How to properly implement a PHP redirect in Drupal 7.x?
body: <p><strong>GOAL:</strong> If the user's point of origin is London, redirect user to a specific landing page/path.</p>
  
  <p><strong>PROBLEM:</strong>
  Implementing the following redirect methods return the following failed results:</p>
  
  <p>Nothing happens >> header("<a href="http://mywebsite/subcat" rel="nofollow noreferrer">http://mywebsite/subcat</a>");</p>
  
  <ol>
  <li>Too many redirects >> header("Location: <a href="http://mywebsite/subcat" rel="nofollow noreferrer">http://mywebsite/subcat</a>");</li>
  <li>Too many redirects >> header("Location: /path");</li>
  <li>Too many redirects >> drupal_goto("<a href="http://mywebsite/subcat" rel="nofollow noreferrer">http://mywebsite/subcat</a>");</li>
  <li>Too many redirects >> drupal_goto("/path");</li>
  <li>Endless loop >> JS:
  window.location.replace("<a href="http://example.com/path" rel="nofollow noreferrer">http://example.com/path</a>");</li>
  </ol>
  
  <p><strong>IMPLEMENTATION:</strong>
  I hacked the below script from a few different SO posts (will give credit as soon as I remember which posts I was searching for) which when used in a stand-alone php works fine (with all the redirect methods listed above), but placed as the first block of code above the header.tpl.php (or properly inside my custom module [using hook_init]), produces the same results listed under PROBLEM. </p>
  
  <p><strong>FUNCTION USED:</strong></p>
  
  <pre><code>        &lt;?php
  
      function get_client_ip ()
      {
          if (!isset ($_SERVER['REMOTE_ADDR'])) {
              return NULL;
          }
  
          $proxy_header = "HTTP_X_FORWARDED_FOR";
              if (array_key_exists ($proxy_header, $_SERVER)) {
                  $client_ip = trim (end (explode (",", $_SERVER[$proxy_header])));
                  if (filter_var ($client_ip, FILTER_VALIDATE_IP)) {
                      return $client_ip;
                  } else {
                      // Validation 
                  }
              }
          return $_SERVER['REMOTE_ADDR'];
      }
  
      $ip = get_client_ip(); // the IP address to query
      $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
      if($query &amp;&amp; $query['status'] == 'success') {
  
  if ($query['country'] == "United Kingdom"){
          header("Location: /mypath");
          } else {
            echo 'Unable to get location';
          }
      }
      ?&gt;
  </code></pre>
  
  <p><strong>ASSUMPTIONS</strong></p>
  
  <ul>
  <li>Logic error in my code.</li>
  <li>Improper use of redirect methods/logic</li>
  <li>Improper placement of my code which gets overridden by the execution
  waterfall of other modules.</li>
  </ul>