Skip to content

Instantly share code, notes, and snippets.

@k1ic
Created April 8, 2015 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k1ic/020e5452050ec5951090 to your computer and use it in GitHub Desktop.
Save k1ic/020e5452050ec5951090 to your computer and use it in GitHub Desktop.
check is domain in host list or is subDomain
/**
* 判断url中的域名是域名列表中的某项或某项的子域名
* @param string $url [必选]
* @return bool
*/
public static function isDomainInListOrIsSubDomain($url = '')
{
$host = parse_url($url, PHP_URL_HOST);
$blackList = array('a.com', 'b.cn', 'c.com.cn');
return in_array($host, $blackList) || self::isSubDomain($host, $blackList);
}
/**
* 判断$host是否为$hostList中某项的子域名
* @param string $host [必选]
* @param array $hostList [必选]
* @return bool
*/
public static function isSubDomain($host = '', $hostList = array())
{
foreach ($hostList as $primaryDomain)
{
preg_match('/^[a-zA-Z0-9\.-]+' . $primaryDomain . '/', $host, $matches);
if (!empty($matches))
{
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment