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原创论坛
Oct 09 PHP
php小经验:解析preg_match与preg_match_all 函数
Jun 29 PHP
YII模块实现绑定二级域名的方法
Jul 09 PHP
ThinkPHP登录功能的实现方法
Aug 20 PHP
PHP中使用sleep函数实现定时任务实例分享
Aug 21 PHP
php实现在站点里面添加邮件发送的功能
Apr 28 PHP
php版微信公众平台接口参数调试实现判断用户行为的方法
Sep 23 PHP
php基于单例模式封装mysql类完整实例
Oct 18 PHP
Yii2实现log输出到file及database的方法
Nov 12 PHP
php实现的简单中文验证码功能示例
Jan 03 PHP
Yii2框架操作数据库的方法分析【以mysql为例】
May 27 PHP
php中字符串和整数比较的操作方法
Jun 06 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
获取焦点时,利用js定时器设定时间执行动作
2010/04/02 Javascript
jQuery封装的tab选项卡插件分享
2015/06/16 Javascript
javascript获取当前的时间戳的方法汇总
2015/07/26 Javascript
JavaScript电子时钟倒计时
2016/01/09 Javascript
javascript中闭包概念与用法深入理解
2016/12/15 Javascript
JavaScript下拉菜单功能实例代码
2017/03/01 Javascript
nodejs入门教程一:概念与用法简介
2017/04/24 NodeJs
vue二级路由设置方法
2018/02/09 Javascript
js实现黑白div块画空心的图形
2018/12/13 Javascript
Vue 2.0双向绑定原理的实现方法
2019/10/23 Javascript
js实现无缝轮播图效果
2020/03/09 Javascript
JS call()及apply()方法使用实例汇总
2020/07/11 Javascript
Python 命令行参数sys.argv
2008/09/06 Python
Python操作MySQL简单实现方法
2015/01/26 Python
用Python实现一个简单的能够上传下载的HTTP服务器
2015/05/05 Python
Python实现Linux的find命令实例分享
2017/06/04 Python
Python中int()函数的用法浅析
2017/10/17 Python
Python实现FTP弱口令扫描器的方法示例
2019/01/31 Python
python Tkinter版学生管理系统
2019/02/20 Python
解决python web项目意外关闭,但占用端口的问题
2019/12/17 Python
python opencv实现图片缺陷检测(讲解直方图以及相关系数对比法)
2020/04/07 Python
jupyter notebook中新建cell的方法与快捷键操作
2020/04/22 Python
Python正则表达式高级使用方法汇总
2020/06/18 Python
python怎么删除缓存文件
2020/07/19 Python
Python如何重新加载模块
2020/07/29 Python
关于HTML5你必须知道的28个新特性,新技巧以及新技术
2012/05/28 HTML / CSS
医生实习工作总结的自我评价
2013/09/27 职场文书
汽车专业大学生职业生涯规划范文
2014/01/07 职场文书
简历中的自我评价怎么写
2014/01/29 职场文书
2014全国两会学习心得体会1000字
2014/03/10 职场文书
公司优秀员工获奖感言
2014/08/14 职场文书
2015年大学学生会工作总结
2015/05/13 职场文书
2015年幼儿园学前班工作总结
2015/05/18 职场文书
法律意见书范文
2015/06/04 职场文书
SpringBoot整合JWT的入门指南
2021/06/29 Java/Android
Redis唯一ID生成器的实现
2022/07/07 Redis