Get Target Host Name From Chttprequest?

You can get most of the necessary stuff from CHttpRequest, but not the request HTTP_HOST header. You can use CHttpRequest::getHostInfo, but this is kind of a hack. I’ve used this with extending CHttpRequest so far, but it might be a good idea to add something like that to Yii.




/**

 * Determines the target host name from request headers

 * @return string|null

 */

public function getTargetHost()

{

   $host = null;

   // Check if the value is set

   if (!empty($_SERVER['HTTP_HOST']))

   {

      $host = $_SERVER['HTTP_HOST'];

   }

   // If HOST header is missing, we're possibly dealing with Apache's mod_proxy

   elseif (isset($_SERVER['HTTP_X_FORWARDED_HOST']))

   {

      if (strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ',') !== FALSE)

      {

         $hostNames = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);

         $host = array_shift($hostNames);

      }

      else

      {

         $host = $_SERVER['HTTP_X_FORWARDED_HOST'];

      }

   }

   return $host;

}