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 相关文章推荐
收集的DedeCMS一些使用经验
Mar 17 PHP
探讨php中防止SQL注入最好的方法是什么
Jun 10 PHP
利用php+mcDropdown实现文件路径可在下拉框选择
Aug 07 PHP
完美解决PHP中的Cannot modify header information 问题
Aug 12 PHP
php使用strtotime和date函数判断日期是否有效代码分享
Dec 25 PHP
WordPress中给文章添加自定义字段及后台编辑功能区域
Dec 19 PHP
thinkphp框架下实现登录、注册、找回密码功能
Apr 06 PHP
Yii2中DropDownList简单用法示例
Jul 18 PHP
浅谈PHP中的Trait使用方法
Mar 22 PHP
php7 list()、session及其他模块的修改实例分析
May 25 PHP
PHP大文件及断点续传下载实现代码
Aug 18 PHP
php实现自动生成验证码的实例讲解
Nov 17 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实现基于文本的摩斯电码生成器
2016/01/11 PHP
微信公众号开发客服接口实例代码
2016/10/21 PHP
PHP使用栈解决约瑟夫环问题算法示例
2017/08/27 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
jquery 简短几句代码实现给元素动态添加及获取提示信息
2011/09/01 Javascript
javascript模版引擎-tmpl的bug修复与性能优化分析
2011/10/23 Javascript
js修改地址栏URL参数解决url参数问题
2012/12/15 Javascript
jQuery阻止事件冒泡具体实现
2013/10/11 Javascript
jQuery实现鼠标经过图片变亮其他变暗效果
2015/05/08 Javascript
jQuery通过写入cookie实现更换网页背景的方法
2016/04/15 Javascript
javascript设计模式之模块模式学习笔记
2017/02/15 Javascript
JavaScript中Promise的使用详解
2017/02/26 Javascript
原生JS实现圣旨卷轴展开效果
2017/03/06 Javascript
React学习笔记之条件渲染(一)
2017/07/02 Javascript
用nodejs实现json和jsonp服务的方法
2017/08/25 NodeJs
[01:50]2014DOTA2西雅图邀请赛 专访欢乐周宝龙
2014/07/08 DOTA
[38:27]完美世界DOTA2联赛PWL S2 Forest vs FTD.C 第二场 11.26
2020/11/30 DOTA
python类继承与子类实例初始化用法分析
2015/04/17 Python
Python自动发邮件脚本
2017/03/31 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
2017/12/12 Python
pyqt 实现在Widgets中显示图片和文字的方法
2019/06/13 Python
python查找重复图片并删除(图片去重)
2019/07/16 Python
利用rest framework搭建Django API过程解析
2019/08/31 Python
Window版下在Jupyter中编写TensorFlow的环境搭建
2020/04/10 Python
面向新手解析python Beautiful Soup基本用法
2020/07/11 Python
python 决策树算法的实现
2020/10/09 Python
AmazeUI 折叠面板的实现代码
2020/08/17 HTML / CSS
乐高积木玩具美国官网:LEGO Shop US
2016/09/16 全球购物
BCBG官网:BCBGMAXAZRIA
2017/12/29 全球购物
德国50岁以上交友网站:Lebensfreunde
2020/03/18 全球购物
优秀护士先进事迹
2014/05/08 职场文书
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
2014年妇产科工作总结
2014/12/08 职场文书
志愿者个人总结
2015/03/03 职场文书
召开会议通知范文
2015/04/15 职场文书
MongoDB数据库之添删改查
2022/04/26 MongoDB