php中取得URL的根域名的代码

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php
/**
* 取得根域名
*
* @author lonely
* @create 2011-3-11
* @version 0.1
* @lastupdate lonely
* @package Sl
*/
class Sl_RootDomain{
private static $self;
private $domain=null;
private $host=null;
private $state_domain;
private $top_domain;
/**
* 取得域名分析实例
* Enter description here ...
*/
public static function instace(){
if(!self::$self)
self::$self=new self();
return self::$self;
}
private function __construct(){
$this->state_domain=array(
'al','dz','af','ar','ae','aw','om','az','eg','et','ie','ee','ad','ao','ai','ag','at','au','mo','bb','pg','bs','pk','py','ps','bh','pa','br','by','bm','bg','mp','bj','be','is','pr','ba','pl','bo','bz','bw','bt','bf','bi','bv','kp','gq','dk','de','tl','tp','tg','dm','do','ru','ec','er','fr','fo','pf','gf','tf','va','ph','fj','fi','cv','fk','gm','cg','cd','co','cr','gg','gd','gl','ge','cu','gp','gu','gy','kz','ht','kr','nl','an','hm','hn','ki','dj','kg','gn','gw','ca','gh','ga','kh','cz','zw','cm','qa','ky','km','ci','kw','cc','hr','ke','ck','lv','ls','la','lb','lt','lr','ly','li','re','lu','rw','ro','mg','im','mv','mt','mw','my','ml','mk','mh','mq','yt','mu','mr','us','um','as','vi','mn','ms','bd','pe','fm','mm','md','ma','mc','mz','mx','nr','np','ni','ne','ng','nu','no','nf','na','za','aq','gs','eu','pw','pn','pt','jp','se','ch','sv','ws','yu','sl','sn','cy','sc','sa','cx','st','sh','kn','lc','sm','pm','vc','lk','sk','si','sj','sz','sd','sr','sb','so','tj','tw','th','tz','to','tc','tt','tn','tv','tr','tm','tk','wf','vu','gt','ve','bn','ug','ua','uy','uz','es','eh','gr','hk','sg','nc','nz','hu','sy','jm','am','ac','ye','iq','ir','il','it','in','id','uk','vg','io','jo','vn','zm','je','td','gi','cl','cf','cn','yr'
);
$this->top_domain=array('com','arpa','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','me','mobi');
$this->url=$_SERVER['HTTP_HOST'];
}
/**
* 设置URL
* Enter description here ...
* @param string $url
*/
public function setUrl($url=null){
$url=$url?$url:$this->url;
if(empty($url))return $this;
if(!preg_match("/^http::/is", $url))
$url="http://".$url;
$url=parse_url(strtolower($url));
$urlarr=explode(".", $url['host']);
$count=count($urlarr);
if ($count<=2){
$this->domain=array_pop($url);
}else if ($count>2){
$last=array_pop($urlarr);
$last_1=array_pop($urlarr);
if(in_array($last, $this->top_domain)){
$this->domain=$last_1.'.'.$last;
$this->host=implode('.', $urlarr);
}else if (in_array($last, $this->state_domain)){
$last_2=array_pop($urlarr);
if(in_array($last_1, $this->top_domain)){
$this->domain=$last_2.'.'.$last_1.'.'.$last;
$this->host=implode('.', $urlarr);
}else{
$this->host=implode('.', $urlarr).$last_2;
$this->domain=$last_1.'.'.$last;
}
}
}
return $this;
}
/**
* 取得域名
* Enter description here ...
*/
public function getDomain(){
return $this->domain;
}
/**
* 取得主机
* Enter description here ...
*/
public function getHost(){
return $this->host;
}
}
?>

相关文章

PHP中使用gettext来支持多语言的方法

我们今天用一个简单的实例说明一下在PHP中的getText的用法(getText是一系列的工具和库函数,帮助程序员和翻译人员开发多语言软件的), 从而实现PHP的i18n. 现在, 我们...

PHP开发不能违背的安全规则 过滤用户输入

作为最基本的防范你需要注意你的外部提交,做好第一面安全机制处理防火墙。 规则 1:绝不要信任外部数据或输入 关于Web应用程序安全性,必须认识到的第一件事是不应该信任外部数据。外部数据(...

深入解析PHP中逗号与点号的区别

复制代码 代码如下: echo 'abc'.'def'; //用点号连接字符串  echo 'abc','def'; //用逗号连接字符串 那么下面我们就举一些例子....

详解PHP的Yii框架中的Controller控制器

控制器是 MVC 模式中的一部分, 是继承yii\base\Controller类的对象,负责处理请求和生成响应。 具体来说,控制器从应用主体接管控制后会分析请求数据并传送到模型, 传送...

PHP中创建和验证哈希的简单方法实探

 PHP 5.5.0 带来了一份完整的全新特性与函数的列表。全新API之一就是Password Hashing API.它包含4个函数:password_get_info(),...