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 20 PHP
php strtotime 函数UNIX时间戳
Jan 14 PHP
使用php来实现网络服务
Sep 15 PHP
php学习之变量的使用
May 29 PHP
php获取网页请求状态程序示例
Jun 17 PHP
PHP采集类Snoopy抓取图片实例
Jun 19 PHP
php中实现精确设置session过期时间的方法
Jul 17 PHP
PHP防止注入攻击实例分析
Nov 03 PHP
PHP6新特性分析
Mar 03 PHP
PHP环境搭建的详细步骤
Jun 30 PHP
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
Mar 26 PHP
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
Mar 29 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函数(简单整理)
2010/04/30 PHP
php中让上传的文件大小在上传前就受限制的两种解决方法
2013/06/24 PHP
PHP中cookie和session的区别实例分析
2014/08/28 PHP
PHP开发中解决并发问题的几种实现方法分析
2017/11/13 PHP
JavaScript Sort 表格排序
2009/10/31 Javascript
location对象的属性和方法应用(解析URL)
2013/04/12 Javascript
常见的javascript跨域通信方法
2015/12/31 Javascript
学习JavaScript设计模式之代理模式
2016/01/12 Javascript
jQuery动态添加可拖动元素完整实例(附demo源码下载)
2016/06/21 Javascript
AngularJS实现DOM元素的显示与隐藏功能
2016/11/22 Javascript
搭建简单的nodejs http服务器详解
2017/03/09 NodeJs
Angular自定义组件实现数据双向数据绑定的实例
2017/12/11 Javascript
SVG实现时钟效果
2018/07/17 Javascript
layui 数据表格复选框实现单选功能的例子
2019/09/19 Javascript
详解element-ui级联菜单(城市三级联动菜单)和回显问题
2019/10/02 Javascript
jQuery实现全选按钮
2021/01/01 jQuery
小程序实现列表倒计时功能
2021/01/29 Javascript
如何在Python函数执行前后增加额外的行为
2016/10/20 Python
python模糊图片过滤的方法
2018/12/14 Python
使用python serial 获取所有的串口名称的实例
2019/07/02 Python
一行Python代码制作动态二维码的实现
2019/09/09 Python
Python龙贝格法求积分实例
2020/02/29 Python
python使用建议与技巧分享(一)
2020/08/17 Python
Ryderwear澳洲官网:澳大利亚高端健身训练装备品牌
2018/09/18 全球购物
Feelunique美国:欧洲大型的在线美妆零售电商
2018/11/04 全球购物
c/c++某大公司的两道笔试题
2014/02/02 面试题
描述RIP和OSPF区别以及特点
2015/01/17 面试题
生产部岗位职责范文
2014/02/07 职场文书
大学生社团活动总结
2014/04/26 职场文书
会计学毕业生求职信
2014/06/25 职场文书
写给医院的感谢信
2015/01/22 职场文书
2015年暑期社会实践总结
2015/07/13 职场文书
年会邀请函的格式及范文五篇
2019/11/02 职场文书
css display table 自适应高度、宽度问题的解决
2021/05/07 HTML / CSS
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB
基于Apache Hudi在Google云构建数据湖平台的思路详解
2022/04/07 Servers