Yii Framework Forum: Get Target Host Name From Chttprequest? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get Target Host Name From Chttprequest? One should be able to access HTTP_HOST request header Rate Topic: -----

#1 User is offline   Ivo Kund 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 21-February 13

Posted 21 February 2013 - 07:51 AM

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;
}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users