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


Posted in PHP onMarch 23, 2011
<?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 相关文章推荐
在PHP中使用curl_init函数的说明
Nov 02 PHP
PHP array_multisort()函数的使用札记
Jul 03 PHP
PHP static局部静态变量和全局静态变量总结
Mar 02 PHP
kohana框架上传文件验证规则写法示例
Jul 14 PHP
php集成环境xampp中apache无法启动问题解决方案
Nov 18 PHP
php获取数组元素中头一个数组元素值的实现方法
Dec 20 PHP
ECSHOP在PHP5.5及高版本上报错的解决方法
Aug 31 PHP
反射调用private方法实践(php、java)
Dec 21 PHP
PHP验证码生成原理和实现
Jan 24 PHP
Thinkphp框架开发移动端接口(1)
Aug 18 PHP
PHP实现的下载远程文件类定义与用法示例
Jul 05 PHP
PHP结合jquery ajax实现上传多张图片,并限制图片大小操作示例
Mar 01 PHP
PHP+JS+rsa数据加密传输实现代码
Mar 23 #PHP
PHP 事件机制(2)
Mar 23 #PHP
php函数之子字符串替换&amp;#65279; str_replace
Mar 23 #PHP
php expects parameter 1 to be resource, array given 错误
Mar 23 #PHP
php去掉字符串的最后一个字符附substr()的用法
Mar 23 #PHP
PHPUnit PHP测试框架安装方法
Mar 23 #PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
Mar 19 #PHP
You might like
PHP中预定义的6种接口介绍
2015/05/12 PHP
thinkphp ajaxfileupload实现异步上传图片的示例
2017/08/28 PHP
javascript比较文档位置
2008/04/08 Javascript
js自动闭合html标签(自动补全html标记)
2012/10/04 Javascript
jQuery UI 实现email输入提示实例
2013/08/15 Javascript
Jquery绑定事件(bind和live的区别介绍)
2013/08/23 Javascript
js中自定义方法实现停留几秒sleep
2014/07/11 Javascript
jQuery子窗体取得父窗体元素的方法
2015/05/11 Javascript
多种JQuery循环滚动文字图片效果代码
2020/06/23 Javascript
全面解析Bootstrap中nav、collapse的使用方法
2016/05/22 Javascript
js实现HashTable(哈希表)的实例分析
2016/11/21 Javascript
jQuery zTree搜索-关键字查询 递归无限层功能实现代码
2018/01/25 jQuery
jQuery实现的页面详情展开收起功能示例
2018/06/11 jQuery
详解微信小程序-扫一扫 wx.scanCode() 扫码大变身
2019/04/30 Javascript
JS操作Fckeditor的一些常用方法(获取、插入等)
2020/02/19 Javascript
详解supervisor使用教程
2017/11/21 Python
谈谈python中GUI的选择
2018/03/01 Python
python中的单引号双引号区别知识点总结
2019/06/23 Python
Python实现PyPDF2处理PDF文件的方法示例
2019/09/25 Python
wxPython修改文本框颜色过程解析
2020/02/14 Python
从多个tfrecord文件中无限读取文件的例子
2020/02/17 Python
Python StringIO及BytesIO包使用方法解析
2020/06/15 Python
python数据类型强制转换实例详解
2020/06/22 Python
详解pytorch中squeeze()和unsqueeze()函数介绍
2020/09/03 Python
Python xlrd/xlwt 创建excel文件及常用操作
2020/09/24 Python
详解基于python的全局与局部序列比对的实现(DNA)
2020/10/07 Python
详解CSS3中使用gradient实现渐变效果的方法
2015/08/18 HTML / CSS
洲际酒店集团美国官网:IHG美国
2017/11/16 全球购物
Timberland法国官网:购买靴子、鞋子、衣服、夹克和配饰
2019/11/30 全球购物
致垒球运动员加油稿
2014/02/16 职场文书
党员活动日总结
2014/05/05 职场文书
乡镇食品安全责任书
2014/07/28 职场文书
文明单位申报材料
2014/12/23 职场文书
创业计划书之熟食店
2019/10/16 职场文书
oracle索引总结
2021/09/25 Oracle
代码复现python目标检测yolo3详解预测
2022/05/06 Python