直接上代码:
/**
* 获取二级域名 如.test.com
*
* @staticvar type $domain
* @return type
*/
public static function getSecondDomain() {
static $domain;
if (empty($domain)) {
$domain = $_SERVER['HTTP_HOST'];
}
$domainArray = explode('.', $domain);
if (count($domainArray) === 2) {//test.com
$domain = '.' . $domain;
} elseif (count($domainArray) === 3) {//www.test.com
$domain = '.' . $domainArray[1] . '.' . $domainArray[2];
}else{//www.test.com.cn
$domain = substr($domain,strpos($domain, '.'));
}
return $domain;
}