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 相关文章推荐
PHP4实际应用经验篇(4)
Oct 09 PHP
利用curl 多线程 模拟 并发的详解
Jun 14 PHP
curl实现站外采集的方法和技巧
Jan 31 PHP
Yii不依赖Model的表单生成器用法实例
Dec 04 PHP
php去除html标记的原生函数详解
Jan 27 PHP
在PHP中使用FastCGI解析漏洞及修复方案
Nov 10 PHP
WordPress中限制非管理员用户在文章后只能评论一次
Dec 31 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
Feb 23 PHP
详解Yii2.0使用AR联表查询实例
Jun 16 PHP
yii2中LinkPager增加总页数和总记录数的实例
Aug 28 PHP
thinkPHP5框架设置404、403等http状态页面的方法
Jun 05 PHP
centos7上编译安装php7以php-fpm方式连接apache
Nov 08 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 面向对象 PHP5 中的常量
2010/05/05 PHP
Thinkphp使用mongodb数据库实现多条件查询方法
2014/06/26 PHP
在html文件中也可以执行php语句的方法
2015/04/09 PHP
js png图片(有含有透明)在IE6中为什么不透明了
2010/02/07 Javascript
function foo的原型与prototype属性解惑
2010/11/19 Javascript
在jQuery 1.5中使用deferred对象的代码(翻译)
2011/03/10 Javascript
httpclient模拟登陆具体实现(使用js设置cookie)
2013/12/11 Javascript
JQuery表单验证插件EasyValidator用法分析
2014/11/15 Javascript
ajax读取数据后使用jqchart显示图表的方法
2015/06/10 Javascript
JavaScript中setMonth()方法的使用详解
2015/06/11 Javascript
Extjs实现下拉菜单效果
2016/04/01 Javascript
基于JavaScript实现在新的tab页打开url
2016/08/04 Javascript
BootStrap实现鼠标悬停下拉列表功能
2017/02/17 Javascript
浅谈js中startsWith 函数不能在任何浏览器兼容的问题
2017/03/01 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
JavaScript函数中的this四种绑定形式
2017/08/15 Javascript
javascript 作用于作用域链的详解
2017/09/27 Javascript
vue的mixins属性详解
2018/03/14 Javascript
JS实现图片上传多次上传同一张不生效的处理方法
2018/08/06 Javascript
微信小程序当前时间时段选择器插件使用方法详解
2018/12/28 Javascript
jQuery判断自定义属性data-val用法示例
2019/01/07 jQuery
详解js 创建对象的几种方法
2019/03/08 Javascript
Vue-CLI 3.X 部署项目至生产服务器的方法
2019/03/22 Javascript
js验证身份证号码记录的方法
2019/04/26 Javascript
JavaScript canvas绘制圆弧与圆形
2020/02/18 Javascript
Python实现的数据结构与算法之快速排序详解
2015/04/22 Python
Python映射拆分操作符用法实例
2015/05/19 Python
Python黑魔法@property装饰器的使用技巧解析
2016/06/16 Python
浅谈Python对内存的使用(深浅拷贝)
2018/01/17 Python
python获取文件路径、文件名、后缀名的实例
2018/04/23 Python
Python利用for循环打印星号三角形的案例
2020/04/12 Python
财产公证书样本
2014/04/04 职场文书
物流管理专业自荐信
2014/06/23 职场文书
关于Javascript闭包与应用的详解
2021/04/22 Javascript
Vite + React从零开始搭建一个开源组件库
2022/06/25 Javascript
Win11右下角图标点了没反应怎么办?Win11点击右下角图标无反应解决方法汇总
2022/07/07 数码科技